def template(cond_shape, x0_shape, x1_shape, description: str = ""): cond = tf.placeholder(np.bool, cond_shape) x0 = tf.placeholder(np.float32, x0_shape, "x0") x1 = tf.placeholder(np.float32, x1_shape, "x1") y = tf.where(cond, x0, x1) vcond = np.random.rand(*cond_shape) > 0.5 vx0 = np.random.rand(*x0_shape).astype(np.float32) vx1 = np.random.rand(*x1_shape).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {cond: vcond, x0: vx0, x1: vx1}) graph = TensorFlowConverter(sess, batch_size=2).convert([cond, x0, x1], [y]) generate_kernel_test_case( description=f"[TensorFlow] Select {description}", graph=graph, backend=["webgpu", "webassembly", "webgl"], inputs={ graph.inputs[0]: vcond, graph.inputs[1]: vx0, graph.inputs[2]: vx1 }, expected={graph.outputs[0]: vy}, )
def template(x_shape=[2, 3, 4, 5], data_format="NHWC", description: str = ""): from tensorflow.python.ops import nn x = tf.placeholder(np.float32, x_shape) scale = tf.placeholder( np.float32, x_shape[-1] if data_format == "NHWC" else x_shape[1]) bias = tf.placeholder(np.float32, x_shape[-1] if data_format == "NHWC" else x_shape[1]) mean = tf.placeholder(np.float32, x_shape[-1] if data_format == "NHWC" else x_shape[1]) variance = tf.placeholder( np.float32, x_shape[-1] if data_format == "NHWC" else x_shape[1]) y, _, _ = nn.fused_batch_norm(x, scale, bias, mean, variance, data_format=data_format, is_training=False) vx = np.random.rand(*x_shape).astype(np.float32) vs = np.random.rand( *[x_shape[-1] if data_format == "NHWC" else x_shape[1]]).astype( np.float32) vb = np.random.rand( *[x_shape[-1] if data_format == "NHWC" else x_shape[1]]).astype( np.float32) vm = np.random.rand( *[x_shape[-1] if data_format == "NHWC" else x_shape[1]]).astype( np.float32) vv = np.random.rand( *[x_shape[-1] if data_format == "NHWC" else x_shape[1]]).astype( np.float32) with tf.Session() as sess: vy, = sess.run([y], { x: vx, scale: vs, bias: vb, mean: vm, variance: vv }) graph = TensorFlowConverter(sess, batch_size=2).convert( [x, scale, bias, mean, variance], [y]) generate_kernel_test_case( description=f"[TensorFlow] FusedBatchNorm {description}", graph=graph, inputs={ graph.inputs[0]: vx, graph.inputs[1]: vs, graph.inputs[2]: vb, graph.inputs[3]: vm, graph.inputs[4]: vv }, expected={graph.outputs[0]: vy}, )
def template(x_shape=[2, 3, 4, 5], description: str = ""): from tensorflow.python.ops import nn x = tf.placeholder(np.float32, x_shape, "x") y = nn.relu6(x) vx = np.random.rand(*x_shape).astype(np.float32) - 0.5 with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) generate_kernel_test_case(description=f"[TensorFlow] Relu6 {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy})
def template(x_shape=[2, 3, 4, 5], description: str = ""): x = tf.placeholder(np.float32, x_shape, "x") y = tf.log(x) vx = np.random.rand(*x_shape).astype(np.float32) + 1 with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) generate_kernel_test_case(description=f"[TensorFlow] Log {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, EPS=1e-2)
def template(x_shape=[2, 3, 4, 5], ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding="VALID", description: str = ""): from tensorflow.python.ops import nn x = tf.placeholder(np.float32, x_shape, "x") y = nn.avg_pool(x, ksize=ksize, strides=strides, padding=padding) vx = np.random.rand(*x_shape).astype(np.float32) - 0.5 with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) generate_kernel_test_case( description=f"[TensorFlow] AvgPool {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy} )
def template(x_shape, description: str = ""): x = tf.placeholder(np.float32, x_shape, "x") y = tf.exp(x) vx = np.random.rand(*x_shape) - 0.5 with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) generate_kernel_test_case( description=f"[TensorFlow] Exp {description}", graph=graph, inputs={graph.inputs[0]: vx, }, expected={graph.outputs[0]: vy}, )
def template(x_shape, description: str = ""): x = tf.placeholder(np.float32, x_shape, "x") y = tf.depth_to_space(x, 2) vx = np.random.rand(*x_shape).astype(np.float32) - 0.5 with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) generate_kernel_test_case( description=f"[TensorFlow] Depth2Space {description}", graph=graph, backend=["webgpu", "webassembly", "webgl"], inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(x_shape=[2, 4, 6, 8], y_shape=[8, 48], description: str = ""): x = tf.placeholder(np.float32, x_shape) y = tf.reshape(x, y_shape) vx = np.random.rand(*x_shape).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) assert list(vy.shape) == list(y.shape) generate_kernel_test_case( description=f"[TensorFlow] Reshape {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(N=2, Hin=5, Hout=3, Win=5, Wout=3, Cin=1, Cout=1, KH=3, KW=3, strides=[1, 1, 1, 1], padding="VALID", data_format="NHWC", description: str = ""): x_shape = [N, Hin, Win, Cin ] if data_format == "NHWC" else [N, Cin, Hin, Win] y_shape = [N, Hout, Wout, Cout ] if data_format == "NHWC" else [N, Cout, Hout, Wout] w_shape = [KH, KW, Cin, Cout] w = tf.placeholder(np.float32, w_shape) gy = tf.placeholder(np.float32, y_shape) x = tf.nn.conv2d_backprop_input(x_shape, w, gy, strides=strides, padding=padding, data_format=data_format) vw = np.random.rand(*w_shape).astype(np.float32) vgy = np.random.rand(*y_shape).astype(np.float32) with tf.Session() as sess: vx, = sess.run([x], {w: vw, gy: vgy}) graph = TensorFlowConverter(sess, batch_size=2).convert([w, gy], [x]) assert list(vx.shape) == list( graph.outputs[0].shape ), f"(vx.shape)={vx.shape}, (graph.outputs[0].shape)={graph.outputs[0].shape}" generate_kernel_test_case( description=f"[TensorFlow] Conv2DBackPropInput {description}", graph=graph, backend=["webgpu", "webassembly", "webgl"], inputs={ graph.inputs[0]: vw, graph.inputs[1]: vgy }, expected={graph.outputs[0]: vx})
def template(start, limit, delta, x1_shape, description: str = ""): x0 = tf.range(start, limit, delta, dtype=np.float32) x1 = tf.placeholder(np.float32, x1_shape) y = x0 + x1 vx1 = np.random.rand(*x1_shape).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x1: vx1}) graph = TensorFlowConverter(sess, batch_size=2).convert([x1], [y]) generate_kernel_test_case( description=f"[TensorFlow] Range {description}", graph=graph, inputs={graph.inputs[0]: vx1}, expected={graph.outputs[0]: vy}, )
def template(x_shape, paddings, constant_values, description: str = ""): x = tf.placeholder(np.float32, x_shape) y = tf.pad(x, paddings, constant_values=constant_values) vx = np.random.rand(*x_shape).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) assert list(vy.shape) == list(y.shape) generate_kernel_test_case( description=f"[TensorFlow] PadV2 {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(x_shape=[2, 4, 6, 8], multiplier=[1, 3, 2, 1], description: str = ""): x = tf.placeholder(np.float32, x_shape) y = tf.tile(x, multiples=multiplier) vx = np.random.rand(*x_shape).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) generate_kernel_test_case( description=f"[TensorFlow] Tile {description}", graph=graph, backend=["webgpu", "webassembly", "webgl"], inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(x_shape, paddings, mode, description: str = ""): x = tf.placeholder(np.float32, x_shape) y = tf.pad(x, paddings, mode=mode) vx = np.random.rand(*x_shape).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) assert list(vy.shape) == list(y.shape) generate_kernel_test_case( description=f"[TensorFlow] MirrorPad {description}", graph=graph, backend=["webgpu", "webassembly", "webgl"], inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(x_shape=[2, 3, 4, 5], pred=True, description: str = ""): from tensorflow.python.ops import control_flow_ops x = tf.placeholder(np.float32, x_shape) y_f, y_t = control_flow_ops.switch(x, pred) y = y_t if pred else y_f vx = np.random.rand(*x_shape).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) generate_kernel_test_case( description=f"[TensorFlow] Switch {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(x: tf.Tensor, y, description: str = ""): vx = np.random.rand(*[dim.value for dim in x.shape.dims]).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) assert list(vy.shape) == list( graph.outputs[0].shape ), f"vy_shape: {vy.shape}, y.shape: {graph.outputs[0].shape}" generate_kernel_test_case( description=f"[TensorFlow] StridedSlice {description}", graph=graph, backend=["webgpu", "webgl", "webassembly"], inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(axis, keep_dims, description: str = ""): x = tf.placeholder(np.float32, [2, 3, 4, 5]) y = tf.reduce_sum(x, axis=axis, keep_dims=keep_dims) vx = np.random.rand(2, 3, 4, 5).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) x = graph.inputs[0] y = graph.outputs[0] assert list(vy.shape) == list(y.shape), f"{vy.shape}, {y.shape}" generate_kernel_test_case( description=f"[TensorFlow] Sum {description}", graph=graph, backend=["webgpu", "webgl", "webassembly"], inputs={x: vx}, expected={y: vy}, )
def template(x0_shape, x1_shape, description: str = ""): x0 = tf.placeholder(np.float32, x0_shape, "x0") x1 = tf.placeholder(np.float32, x1_shape, "x1") y = x0 * x1 vx0 = np.random.rand(*x0_shape).astype(np.float32) vx1 = np.random.rand(*x1_shape).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x0: vx0, x1: vx1}) graph = TensorFlowConverter(sess, batch_size=2).convert([x0, x1], [y]) generate_kernel_test_case( description=f"[TensorFlow] Mul {description}", graph=graph, inputs={ graph.inputs[0]: vx0, graph.inputs[1]: vx1 }, expected={graph.outputs[0]: vy}, )
def template(x_shape=[2, 3, 4, 5], x_order=OrderNHWC, axis=Axis.C, keep_dims=False, description: str = ""): x = tf.placeholder(np.float32, x_shape) y = tf.reduce_min(x, axis=x_order.axes_dict[axis], keep_dims=keep_dims) vx = np.random.rand(*x_shape).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) assert list(vy.shape) == list(graph.outputs[0].shape) generate_kernel_test_case( description=f"[TensorFlow] Min {description}", graph=graph, backend=["webgpu", "webassembly", "webgl"], inputs={ graph.inputs[0]: vx }, expected={graph.outputs[0]: vy}, )
def template(x_shape, y_shape, description: str = ""): x = tf.placeholder(np.float32, x_shape, "x") y = tf.placeholder(np.float32, y_shape, "y") z = tf.squared_difference(x, y) vx = np.random.rand(*x_shape).astype(np.float32) vy = np.random.rand(*y_shape).astype(np.float32) with tf.Session() as sess: vz, = sess.run([z], {x: vx, y: vy}) graph = TensorFlowConverter(sess, batch_size=2).convert([x, y], [z]) generate_kernel_test_case( description=f"[TensorFlow] SquaredDifference {description}", graph=graph, inputs={ graph.inputs[0]: vx, graph.inputs[1]: vy }, expected={graph.outputs[0]: vz}, )
def template(x_shape, begin, size, description: str = ""): x = tf.placeholder(np.float32, x_shape) y = tf.slice(x, begin, size) vx = np.random.rand(*x_shape).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) assert list(vy.shape) == list( graph.outputs[0].shape ), f"vy_shape: {vy.shape}, y.shape: {graph.outputs[0].shape}" generate_kernel_test_case( description=f"[TensorFlow] Slice {description}", graph=graph, backend=["webgpu", "webgl", "webassembly"], inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(N=2, H=5, W=5, Cin=7, Cout=9, KH=3, KW=3, strides=[1, 1, 1, 1], padding="VALID", data_format="NHWC", description: str = ""): x_shape = [N, H, W, Cin] if data_format == "NHWC" else [N, Cin, H, W] w_shape = [KH, KW, Cin, Cout] x = tf.placeholder(np.float32, x_shape) w = tf.placeholder(np.float32, w_shape) y = tf.nn.conv2d(x, w, strides=strides, padding=padding, data_format=data_format) vx = np.random.rand(*x_shape).astype(np.float32) vw = np.random.rand(*w_shape).astype(np.float32) with tf.Session() as sess: vy, = sess.run([y], {x: vx, w: vw}) graph = TensorFlowConverter(sess, batch_size=2).convert([x, w], [y]) assert list(vy.shape) == list( graph.outputs[0].shape ), f"(vy.shape)={vy.shape}, (graph.outputs[0].shape)={graph.outputs[0].shape}" generate_kernel_test_case(description=f"[TensorFlow] Conv2D {description}", graph=graph, inputs={ graph.inputs[0]: vx, graph.inputs[1]: vw }, expected={graph.outputs[0]: vy})
def template(x_shape=[2, 3, 4, 5], data_format="NHWC", description: str = ""): from tensorflow.python.ops import nn x = tf.placeholder(np.float32, x_shape) b = tf.placeholder(np.float32, x_shape[-1] if data_format == "NHWC" else x_shape[1]) y = nn.bias_add(x, b, data_format) vx = np.random.rand(*x_shape).astype(np.float32) vb = np.random.rand( *[x_shape[-1] if data_format == "NHWC" else x_shape[1]]).astype( np.float32) with tf.Session() as sess: vy, = sess.run([y], {x: vx, b: vb}) graph = TensorFlowConverter(sess, batch_size=2).convert([x, b], [y]) generate_kernel_test_case( description=f"[TensorFlow] BiasAdd {description}", graph=graph, inputs={ graph.inputs[0]: vx, graph.inputs[1]: vb }, expected={graph.outputs[0]: vy}, )