Esempio n. 1
0
          |
          |
          | dynamic tensor at compilation time but the shape will be [1, 3, 4, 1] at execution time
          |
      ReduceMean
          |
        output (dynamic tensor, [1, 3, 1] at execution time)
'''

import dynamic_tensor

model = Model()

model_input_shape = [1, 3, 4, 1]

dynamic_layer = dynamic_tensor.DynamicInputGenerator(model, model_input_shape,
                                                     "TENSOR_FLOAT32")
test_node_input = dynamic_layer.getTestNodeInput()

# write REDUCE_MEAN test. input is `test_input`

# note output shape is used by expected output's shape
model_output = Output("output", "TENSOR_FLOAT32", "{1, 3, 1}")

axis = Int32Scalar("axis", 2)
keepDims = Int32Scalar("axis", 0)

model.Operation("MEAN", test_node_input, axis, keepDims).To(model_output)

model_input_data = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4.0]

model_output_data = [2.5, 2.5, 2.5]
# model
import dynamic_tensor
model = Model()

start_shape = [1]
dynamic_start = dynamic_tensor.DynamicInputGenerator(model, start_shape)
test_start_input = dynamic_start.getTestNodeInput()

limit_shape = [1]
dynamic_limit = dynamic_tensor.DynamicInputGenerator(model, limit_shape)
test_limit_input = dynamic_limit.getTestNodeInput()

delta_shape = [1]
dynamic_delta = dynamic_tensor.DynamicInputGenerator(model, delta_shape)
test_delta_input = dynamic_delta.getTestNodeInput()

out = Output("output", "TENSOR_FLOAT32", "{3}")
model = model.Operation("RANGE_EX", test_start_input, test_limit_input,
                        test_delta_input).To(out)

test_start_data = [10]
test_limit_data = [3]
test_delta_data = [-3]
output_data = [10.0, 7.0, 4.0]

# Instantiate an example
Example({
    dynamic_start.getModelInput(): test_start_data,
    dynamic_start.getShapeInput(): start_shape,
    dynamic_limit.getModelInput(): test_limit_data,
    dynamic_limit.getShapeInput(): limit_shape,
o1_data = [
    1,
    2,
    1,
    2,
    1,
    2,
    3,
    4,
    3,
    4,
    3,
    4,
]

dynamic_layer = dynamic_tensor.DynamicInputGenerator(model, i1_shape,
                                                     "TENSOR_INT32")

i1 = dynamic_layer.getTestNodeInput()
i2 = Input("input2", "TENSOR_INT32", "{3}")
o1 = Output("output0", "TENSOR_INT32", "{2, 3, 2}")

model = model.Operation("BROADCAST_TO_EX", i1, i2).To(o1)

# Example.
input0 = {
    dynamic_layer.getModelInput(): i1_data,
    dynamic_layer.getShapeInput(): i1_shape,
    i2: i2_data
}

output0 = {o1: o1_data}
Esempio n. 4
0
# Log:
#
# [Linear] * OP_SEQ {cpu}   OpSequence IN(0,1) -> { 0(Reshape:0,1:2) 1(Add:2,3:5) 2(Add:5,3:6) } -> OUT(6)
# [StaticInferer] Operand #5, Dynamic, shape : {1}
# [StaticInferer] Operand #1, Static, shape : {2}
# [StaticInferer] Operand #6, Dynamic, shape : {3 4}
# [StaticInferer] Operand #0, Static, shape : {3 4}
# [StaticInferer] Operand #2, Dynamic, shape : {1}
# [StaticInferer] Operand #3, Static, shape : {1 4}
# [StaticInferer] Operand #4, Static, shape : {}

model = Model()

model_input1_shape = [3, 4]  # first input shape of Add. 12 float32s

dynamic_layer = dynamic_tensor.DynamicInputGenerator(model, model_input1_shape)

test_node_input = dynamic_layer.getTestNodeInput(
)  # first input of Add is dynamic tensor

# model definition
i2 = Input("op2", "TENSOR_FLOAT32",
           "{1, 4}")  # second input of Add. 4 float32s
t1 = Internal("op3", "TENSOR_FLOAT32",
              "{}")  # result of first Add. dynamic and shape is not known
act = Int32Scalar("act", 0)  # an int32_t scalar activation
o1 = Output("op3", "TENSOR_FLOAT32", "{3, 4}")

model = model.Operation("ADD", test_node_input, i2, act).To(t1)  # first add
model = model.Operation("ADD", t1, i2, act).To(o1)  # second add