use of org.apache.sysml.runtime.DMLRuntimeException in project incubator-systemml by apache.
the class ExternalFunctionProgramBlock method verifyAndAttachOutputs.
/**
* Method to verify that function outputs match with declared outputs
*
* @param ec execution context
* @param returnFunc package function
* @param outputParams output parameters
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
protected void verifyAndAttachOutputs(ExecutionContext ec, PackageFunction returnFunc, String outputParams) throws DMLRuntimeException {
ArrayList<String> outputs = getParameters(outputParams);
if (outputs.size() != returnFunc.getNumFunctionOutputs()) {
throw new DMLRuntimeException("Number of function outputs (" + returnFunc.getNumFunctionOutputs() + ") " + "does not match with declaration (" + outputs.size() + ").");
}
// iterate over each output and verify that type matches
for (int i = 0; i < outputs.size(); i++) {
StringTokenizer tk = new StringTokenizer(outputs.get(i), ":");
ArrayList<String> tokens = new ArrayList<String>();
while (tk.hasMoreTokens()) {
tokens.add(tk.nextToken());
}
if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Matrix) {
Matrix m = (Matrix) returnFunc.getFunctionOutput(i);
if (!(tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Matrix))) || !(tokens.get(2).equals(getMatrixValueTypeString(m.getValueType())))) {
throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
}
// add result to variableMapping
String varName = tokens.get(1);
MatrixObject newVar = createOutputMatrixObject(m);
newVar.setVarName(varName);
//getVariables().put(varName, newVar); //put/override in local symbol table
ec.setVariable(varName, newVar);
continue;
}
if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Scalar) {
Scalar s = (Scalar) returnFunc.getFunctionOutput(i);
if (!tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Scalar)) || !tokens.get(2).equals(getScalarValueTypeString(s.getScalarType()))) {
throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
}
// allocate and set appropriate object based on type
ScalarObject scalarObject = null;
ScalarValueType type = s.getScalarType();
switch(type) {
case Integer:
scalarObject = new IntObject(tokens.get(1), Long.parseLong(s.getValue()));
break;
case Double:
scalarObject = new DoubleObject(tokens.get(1), Double.parseDouble(s.getValue()));
break;
case Boolean:
scalarObject = new BooleanObject(tokens.get(1), Boolean.parseBoolean(s.getValue()));
break;
case Text:
scalarObject = new StringObject(tokens.get(1), s.getValue());
break;
default:
throw new DMLRuntimeException("Unknown scalar value type '" + type + "' of output '" + outputs.get(i) + "'.");
}
//this.getVariables().put(tokens.get(1), scalarObject);
ec.setVariable(tokens.get(1), scalarObject);
continue;
}
if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Object) {
if (!tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Object))) {
throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
}
throw new DMLRuntimeException("Object types not yet supported");
// continue;
}
throw new DMLRuntimeException("Unknown data type '" + returnFunc.getFunctionOutput(i).getType() + "' " + "of output '" + outputs.get(i) + "'.");
}
}
use of org.apache.sysml.runtime.DMLRuntimeException in project incubator-systemml by apache.
the class ExternalFunctionProgramBlock method executeInstruction.
/**
* Method to execute an external function invocation instruction.
*
* @param ec execution context
* @param inst external function invocation instructions
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
@SuppressWarnings("unchecked")
public void executeInstruction(ExecutionContext ec, ExternalFunctionInvocationInstruction inst) throws DMLRuntimeException {
String className = inst.getClassName();
String configFile = inst.getConfigFile();
if (className == null)
throw new DMLRuntimeException(this.printBlockErrorLocation() + "Class name can't be null");
// create instance of package function.
Object o;
try {
Class<Instruction> cla = (Class<Instruction>) Class.forName(className);
o = cla.newInstance();
} catch (Exception e) {
throw new DMLRuntimeException(this.printBlockErrorLocation() + "Error generating package function object ", e);
}
if (!(o instanceof PackageFunction))
throw new DMLRuntimeException(this.printBlockErrorLocation() + "Class is not of type PackageFunction");
PackageFunction func = (PackageFunction) o;
// add inputs to this package function based on input parameter
// and their mappings.
setupInputs(func, inst.getInputParams(), ec.getVariables());
func.setConfiguration(configFile);
func.setBaseDir(_baseDir);
//executes function
func.execute();
// verify output of function execution matches declaration
// and add outputs to variableMapping and Metadata
verifyAndAttachOutputs(ec, func, inst.getOutputParams());
}
use of org.apache.sysml.runtime.DMLRuntimeException in project incubator-systemml by apache.
the class ScalarScalarBuiltinCPInstruction method processInstruction.
@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
ScalarObject so1 = ec.getScalarInput(input1.getName(), input1.getValueType(), input1.isLiteral());
ScalarObject so2 = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral());
BinaryOperator dop = (BinaryOperator) _optr;
ScalarObject sores = null;
//compute output value, incl implicit type promotion if necessary
if (so1 instanceof StringObject || so2 instanceof StringObject)
throw new DMLRuntimeException("Binary builtin '" + getOpcode() + "' not supported over string inputs.");
else if (so1 instanceof DoubleObject || so2 instanceof DoubleObject || output.getValueType() == ValueType.DOUBLE)
sores = new DoubleObject(dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()));
else if (so1 instanceof IntObject || so2 instanceof IntObject)
sores = new IntObject((long) dop.fn.execute(so1.getLongValue(), so2.getLongValue()));
else
//all boolean
throw new DMLRuntimeException("Binary builtin '" + getOpcode() + "' not supported over boolean inputs.");
ec.setScalarOutput(output.getName(), sores);
}
use of org.apache.sysml.runtime.DMLRuntimeException in project incubator-systemml by apache.
the class ScalarScalarArithmeticCPInstruction method processInstruction.
@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
ScalarObject so1 = ec.getScalarInput(input1.getName(), input1.getValueType(), input1.isLiteral());
ScalarObject so2 = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral());
BinaryOperator dop = (BinaryOperator) _optr;
ScalarObject sores = null;
//compute output value, incl implicit type promotion if necessary
if (so1 instanceof StringObject || so2 instanceof StringObject) {
if (//not string concatenation
!getOpcode().equals("+"))
throw new DMLRuntimeException("Arithmetic '" + getOpcode() + "' not supported over string inputs.");
sores = new StringObject(dop.fn.execute(so1.getLanguageSpecificStringValue(), so2.getLanguageSpecificStringValue()));
} else if (so1 instanceof DoubleObject || so2 instanceof DoubleObject || output.getValueType() == ValueType.DOUBLE) {
sores = new DoubleObject(dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()));
} else if (so1 instanceof IntObject || so2 instanceof IntObject) {
double tmp = dop.fn.execute(so1.getLongValue(), so2.getLongValue());
if (//cast to long if no overflow, otherwise controlled exception
tmp > Long.MAX_VALUE)
throw new DMLRuntimeException("Integer operation created numerical result overflow (" + tmp + " > " + Long.MAX_VALUE + ").");
sores = new IntObject((long) tmp);
} else {
//all boolean
//NOTE: boolean-boolean arithmetic treated as double for consistency with R
sores = new DoubleObject(dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()));
}
ec.setScalarOutput(output.getName(), sores);
}
use of org.apache.sysml.runtime.DMLRuntimeException in project incubator-systemml by apache.
the class LibMatrixCUDA method maxpoolingBackward.
/**
* Performs maxpoolingBackward on GPU by exploiting cudnnPoolingBackward(...)
* This method computes the backpropogation errors for previous layer of maxpooling operation
* @param gCtx a valid {@link GPUContext}
* @param instName the invoking instruction's name for record {@link Statistics}.
* @param image image as matrix object
* @param dout delta matrix, output of previous layer
* @param outputBlock output matrix
* @param N batch size
* @param C number of channels
* @param H height of image
* @param W width of image
* @param K number of filters
* @param R height of filter
* @param S width of filter
* @param pad_h vertical padding
* @param pad_w horizontal padding
* @param stride_h horizontal stride
* @param stride_w vertical stride
* @param P (H - R + 1 + 2*pad_h)/stride_h
* @param Q (W - S + 1 + 2*pad_w)/stride_w
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
public static void maxpoolingBackward(GPUContext gCtx, String instName, MatrixObject image, MatrixObject dout, MatrixObject outputBlock, int N, int C, int H, int W, int K, int R, int S, int pad_h, int pad_w, int stride_h, int stride_w, int P, int Q) throws DMLRuntimeException {
LOG.trace("GPU : maxpoolingBackward" + ", GPUContext=" + gCtx);
Pointer y = null;
cudnnPoolingDescriptor poolingDesc = null;
try {
long t1 = 0, t2 = 0, t3 = 0;
if (GPUStatistics.DISPLAY_STATISTICS)
t1 = System.nanoTime();
// Allocate descriptors
cudnnTensorDescriptor xDesc = allocateTensorDescriptor(gCtx, image, N, C, H, W);
cudnnTensorDescriptor yDesc = allocateTensorDescriptor(gCtx, dout, N, C, P, Q);
cudnnTensorDescriptor dxDesc = allocateTensorDescriptor(gCtx, outputBlock, N, C, H, W);
cudnnTensorDescriptor dyDesc = allocateTensorDescriptor(gCtx, dout, N, C, P, Q);
poolingDesc = allocatePoolingDescriptor(R, S, pad_h, pad_w, stride_h, stride_w);
// Calling PoolForward first, y is one of the inputs for poolBackward
// TODO: Remove calling poolForward after necessary changes at language level for poolBackward
long numBytes = N * C * P * Q * Sizeof.DOUBLE;
y = gCtx.allocate(numBytes);
// Allocate data
Pointer x = getDensePointer(gCtx, image, true, instName);
Pointer dx = getDensePointer(gCtx, outputBlock, true, instName);
Pointer dy = getDensePointer(gCtx, dout, true, instName);
if (GPUStatistics.DISPLAY_STATISTICS)
GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDNN_INIT, System.nanoTime() - t1);
if (GPUStatistics.DISPLAY_STATISTICS)
t2 = System.nanoTime();
int status = cudnnPoolingForward(getCudnnHandle(gCtx), poolingDesc, one(), xDesc, x, zero(), yDesc, y);
if (GPUStatistics.DISPLAY_STATISTICS)
GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_MAXPOOLING_FORWARD_LIB, System.nanoTime() - t2);
if (status != jcuda.jcudnn.cudnnStatus.CUDNN_STATUS_SUCCESS) {
throw new DMLRuntimeException("Could not executed cudnnPoolingForward before cudnnPoolingBackward: " + jcuda.jcudnn.cudnnStatus.stringFor(status));
}
if (GPUStatistics.DISPLAY_STATISTICS)
t3 = System.nanoTime();
status = cudnnPoolingBackward(getCudnnHandle(gCtx), poolingDesc, one(), yDesc, y, dyDesc, dy, xDesc, x, zero(), dxDesc, dx);
if (GPUStatistics.DISPLAY_STATISTICS)
GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_MAXPOOLING_BACKWARD_LIB, System.nanoTime() - t3);
if (status != jcuda.jcudnn.cudnnStatus.CUDNN_STATUS_SUCCESS) {
throw new DMLRuntimeException("Could not executed cudnnPoolingBackward: " + jcuda.jcudnn.cudnnStatus.stringFor(status));
}
} catch (CudaException e) {
throw new DMLRuntimeException("Error in conv2d in GPUContext " + gCtx.toString() + " from Thread " + Thread.currentThread().toString(), e);
} finally {
long t4 = 0;
if (GPUStatistics.DISPLAY_STATISTICS)
t4 = System.nanoTime();
if (y != null)
gCtx.cudaFreeHelper(instName, y);
if (poolingDesc != null)
cudnnDestroyPoolingDescriptor(poolingDesc);
if (GPUStatistics.DISPLAY_STATISTICS)
GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDNN_CLEANUP, System.nanoTime() - t4);
}
}
Aggregations