org.nd4j.linalg.api.ops.BaseOpContext Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
package org.nd4j.linalg.api.ops;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.val;
import org.nd4j.linalg.api.buffer.DataType;
import org.nd4j.linalg.api.ndarray.INDArray;
import java.util.*;
/**
* Implementation of common methods for OpContext
*
* @author [email protected]
*/
public abstract class BaseOpContext implements OpContext {
protected Map fastpath_in = new HashMap<>();
protected Map fastpath_out = new HashMap<>();
protected List fastpath_t = new ArrayList<>();
protected List fastpath_b = new ArrayList<>();
protected List fastpath_i = new ArrayList<>();
protected List fastpath_d = new ArrayList<>();
@Setter()
@Getter
protected ExecutionMode executionMode = ExecutionMode.UNDEFINED;
@Override
public void setIArguments(long... arguments) {
fastpath_i.clear();
for (val v:arguments)
fastpath_i.add(v);
}
@Override
public List getIArguments(){
return fastpath_i;
}
@Override
public int numIArguments() {
return fastpath_i.size();
}
@Override
public void setTArguments(double... arguments) {
fastpath_t.clear();
for (val v:arguments)
fastpath_t.add(v);
}
@Override
public List getTArguments(){
return fastpath_t;
}
@Override
public int numTArguments() {
return fastpath_t.size();
}
@Override
public void setBArguments(boolean... arguments) {
fastpath_b.clear();
for (val v:arguments)
fastpath_b.add(v);
}
@Override
public List getBArguments(){
return fastpath_b;
}
@Override
public int numBArguments() {
return fastpath_b.size();
}
@Override
public void setDArguments(DataType... arguments) {
fastpath_d.clear();
for (val v:arguments)
fastpath_d.add(v);
}
@Override
public List getDArguments() {
return fastpath_d;
}
@Override
public int numDArguments() {
return fastpath_d.size();
}
@Override
public void setInputArray(int index, @NonNull INDArray array) {
fastpath_in.put(index, array);
}
@Override
public List getInputArrays() {
val result = new ArrayList();
for (int e = 0; e < Integer.MAX_VALUE; e++) {
val arr = fastpath_in.get(e);
if (arr != null)
result.add(arr);
else
break;
}
return result;
}
@Override
public int numInputArguments() {
return fastpath_in.size();
}
@Override
public INDArray getInputArray(int idx) {
return fastpath_in.get(idx);
}
@Override
public List getOutputArrays() {
val result = new ArrayList();
for (int e = 0; e < Integer.MAX_VALUE; e++) {
val arr = fastpath_out.get(e);
if (arr != null)
result.add(arr);
else
break;
}
return result;
}
@Override
public void setOutputArray(int index, @NonNull INDArray array) {
fastpath_out.put(index, array);
}
@Override
public INDArray getOutputArray(int i) {
return fastpath_out.get(i);
}
@Override
public int numOutputArguments() {
return fastpath_out.size();
}
@Override
public void setInputArrays(@NonNull List arrays) {
for (int e = 0; e < arrays.size(); e++)
setInputArray(e, arrays.get(e));
}
@Override
public void setOutputArrays(@NonNull List arrays) {
for (int e = 0; e < arrays.size(); e++)
setOutputArray(e, arrays.get(e));
}
@Override
public void setInputArrays(INDArray... arrays) {
for (int e = 0; e < arrays.length; e++)
setInputArray(e, arrays[e]);
}
@Override
public void setOutputArrays(INDArray... arrays) {
for (int e = 0; e < arrays.length; e++)
setOutputArray(e, arrays[e]);
}
@Override
public void purge() {
fastpath_in.clear();
fastpath_out.clear();
}
}