def test(): for kwargs in [ {}, # {"use_bias": False}, # FIXME: Not Supported Yet # {"stateful": True}, # FIXME: Not Supported Yet # {"go_backwards": True}, # FIXME: Not Supported Yet {"recurrent_activation": "hard_sigmoid"}, {"recurrent_activation": "sigmoid"}, {"return_state": True}, {"return_sequences": True}, {"return_state": True, "return_sequences": True}, ]: return_state = ("return_state" in kwargs) and (kwargs["return_state"]) x = keras.layers.Input((14, 15)) vx = np.random.rand(2, 14, 15) outputs = keras.layers.LSTM(units=16, **kwargs)(x) if return_state: y, _, c = outputs model = keras.models.Model([x], [y, c]) graph = KerasConverter(batch_size=2).convert(model) vy, vc = model.predict(vx, batch_size=2) expected = { graph.outputs[0]: vy, graph.outputs[1]: vc, } else: y = outputs model = keras.models.Model([x], [y]) graph = KerasConverter(batch_size=2).convert(model) vy = model.predict(vx, batch_size=2) expected = { graph.outputs[0]: vy, } generate_kernel_test_case( description="[keras] LSTM " + (", ".join([f"{k}={v}" for k, v in kwargs.items()])), graph=graph, backend=["webgpu", "webassembly"], # FIXME: fallback backend doesn't support LSTM inputs={graph.inputs[0]: vx}, expected=expected, raise_skip=False ) raise SkipTest
def template(units=16, return_sequences=False, return_state=False, go_backwards=False, stateful=False, activation="tanh", recurrent_activation="hard_sigmoid", use_bias=True, description: str = ""): x = keras.layers.Input((14, 15)) vx = np.random.rand(2, 14, 15).astype(np.float32) outputs = keras.layers.LSTM(units=units, return_sequences=return_sequences, return_state=return_state, go_backwards=go_backwards, stateful=stateful, activation=activation, recurrent_activation=recurrent_activation, use_bias=use_bias)(x) if return_state: y, _, c = outputs model = keras.models.Model([x], [y, c]) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) vy, vc = model.predict(vx, batch_size=2) expected = { graph.outputs[0]: vy, graph.outputs[1]: vc, } else: y = outputs model = keras.models.Model([x], [y]) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) vy = model.predict(vx, batch_size=2) expected = { graph.outputs[0]: vy, } generate_kernel_test_case(description=f"[keras] LSTM {description}", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx}, expected=expected, EPS=1e-2)
def template(pool_size=(3, 3), strides=2, padding="valid", data_format=None, description: str = ""): x = keras.layers.Input( (15, 17, 16) ) # (height + pad * 2 - pool_size) % stride == 0 to avoid edge difference y = keras.layers.AveragePooling2D(pool_size=pool_size, strides=strides, padding=padding, data_format=data_format)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 15, 17, 16) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) generate_kernel_test_case( description=f"[keras] AveragePooling2D {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def test(): for kwargs in [ # {"data_format": "channels_first"}, # FIXME: Not Supported Yet { "data_format": "channels_last" }, ]: channels_first = ("data_format" in kwargs) and (kwargs["data_format"] == "channels_first") x = keras.layers.Input((14, 15, 16)) y = keras.layers.GlobalAveragePooling2D(**kwargs)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 14, 15, 16) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert( model, input_orders=[OrderNCHW if channels_first else OrderNHWC]) generate_kernel_test_case( description="[keras] GlobalAveragePooling2D " + (", ".join([f"{k}={v}" for k, v in kwargs.items()])), graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, raise_skip=False) raise SkipTest
def template(x1_shape=[5, 10, 15], x2_shape=[5, 9, 15], axis=2, description: str = ""): x1 = keras.layers.Input(x1_shape) x2 = keras.layers.Input(x2_shape) y = keras.layers.Concatenate(axis=axis)([x1, x2]) model = keras.models.Model([x1, x2], [y]) vx1 = np.random.rand(2, *x1_shape) vx2 = np.random.rand(2, *x2_shape) vy = model.predict([vx1, vx2], batch_size=2) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) generate_kernel_test_case( description=f"[keras] Concat {description}", graph=graph, inputs={ graph.inputs[0]: vx1, graph.inputs[1]: vx2 }, expected={graph.outputs[0]: vy}, )
def test(): for kwargs in [ {"output_dim": 1}, {"output_dim": 10}, ]: N_VOCABULARY = 30 x = keras.layers.Input((14,)) y = keras.layers.Embedding(input_dim=N_VOCABULARY, **kwargs)(x) model = keras.models.Model([x], [y]) vx = np.random.randint(low=0, high=N_VOCABULARY, size=(2, 14)) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case( description="[keras] Embedding " + (", ".join([f"{k}={v}" for k, v in kwargs.items()])), graph=graph, backend=["webgpu", "webassembly"], # FIXME: fallback backend doesn't support ZeroPadding1D inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, raise_skip=False ) raise SkipTest
def test(): for kwargs in [ {}, {"use_bias": False}, {"activation": None}, {"use_bias": False, "activation": None} ]: x = keras.layers.Input((4,)) y = keras.layers.Dense(8, **kwargs)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 4) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case( description="[keras] Dense " + (", ".join([f"{k}={v}" for k, v in kwargs.items()])), graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, raise_skip=False ) raise SkipTest
def template(filters=17, kernel_size=3, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, description: str = ""): x = keras.layers.Input((14, 15, 16)) y = keras.layers.Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activation, use_bias=use_bias)(x) model = keras.models.Model([x], [y]) vx = np.random.randint(low=0, high=100, size=(2, 14, 15, 16)).astype(np.float32) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert( model, input_orders=[ OrderNCHW if data_format == "channels_first" else OrderNHWC ]) generate_kernel_test_case(description=f"[keras] Conv2D {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, EPS=1e-2)
def template(pool_size=(3, 3), shape=(15, 17, 16), strides=2, padding="valid", data_format=None, description: str = ""): x = keras.layers.Input(shape) y = keras.layers.MaxPooling2D(pool_size=pool_size, strides=strides, padding=padding, data_format=data_format)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, *shape) - 0.5 vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) 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"[keras] MaxPooling2D {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def test(): for kwargs in [ {}, { "epsilon": 0.8 }, { "scale": False }, { "center": False }, ]: x = keras.layers.Input((14, 15, 16)) y = keras.layers.BatchNormalization(axis=3, **kwargs)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 14, 15, 16) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case( description="[keras] BatchNormalization " + (", ".join([f"{k}={v}" for k, v in kwargs.items()])), graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, raise_skip=False) raise SkipTest
def template(shape=(14, 15, 4), filters=5, kernel_size=3, strides=(1, 1), padding='valid', data_format="channels_last", dilation_rate=(1, 1), activation=None, depth_multiplier=1, use_bias=True, description: str = ""): x = keras.layers.Input(shape) y = keras.layers.SeparableConv2D(filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, depth_multiplier=depth_multiplier, activation=activation, use_bias=use_bias)(x) model = keras.models.Model([x], [y]) vx = np.random.randint(low=0, high=100, size=(2, *shape)).astype(np.float32) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) generate_kernel_test_case( description=f"[keras] SeparableConv2D {description}", graph=graph, backend=["webgpu", "webgl", "webassembly"], inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, # TODO: replace computation algorithm with more accurate one EPS=1e-2 )
def test(): x = keras.layers.Input((14, )) y = keras.layers.RepeatVector(n=5)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 14) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case(description="[keras] Reshape", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy})
def test(): x = keras.layers.Input((3, 4, 5)) y = keras.layers.Flatten()(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 3, 4, 5) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case(description="[keras] Flatten", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy})
def test(): x = keras.layers.Input((14, 15)) y = keras.layers.GlobalAveragePooling1D()(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 14, 15) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case(description="[keras] GlobalAveragePooling1D", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy})
def template(activation="", EPS=1e-5, description: str = ""): x = keras.layers.Input((4, )) y = keras.layers.Activation(activation)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 4) - 0.5 vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case(description=f"[keras] Activation {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, EPS=EPS)
def template(alpha=1.0, description: str = ""): x = keras.layers.Input((4, )) y = keras.layers.ELU(alpha=alpha)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 4).astype(np.float32) - 0.5 vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case( description=f"[keras] Elu {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(description: str = ""): x = keras.layers.Input((3, 4, 5)) y = keras.layers.Flatten()(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 3, 4, 5) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) generate_kernel_test_case( description=f"[keras] Flatten {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(activation=None, use_bias=True, description: str = ""): x = keras.layers.Input((4,)) y = keras.layers.Dense(8, activation=activation, use_bias=use_bias)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 4) - 0.5 vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) generate_kernel_test_case( description=f"[keras] Dense {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(axis=-1, epsilon=1e-3, center=True, scale=True, description: str = ""): x = keras.layers.Input((14, 15, 16)) y = keras.layers.BatchNormalization(axis=axis, epsilon=epsilon, center=center, scale=scale)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 14, 15, 16) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) generate_kernel_test_case( description=f"[keras] BatchNormalization {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(pool_size=3, strides=1, padding="valid", description: str = ""): x = keras.layers.Input((14, 15)) y = keras.layers.AveragePooling1D(pool_size=pool_size, strides=strides, padding=padding)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 14, 15) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) generate_kernel_test_case( description=f"[keras] AveragePooling1D {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(theta=1.0, description: str = ""): x = keras.layers.Input((4,)) y = keras.layers.ThresholdedReLU(theta=theta)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 4) - 0.5 vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) generate_kernel_test_case( description=f"[keras] ThresholdedReLU {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(description: str = ""): x = keras.layers.Input((4, 5, 6)) y = keras.layers.Reshape(target_shape=(12, 10))(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 4, 5, 6) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case( description=f"[keras] Reshape {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(description: str = ""): x = keras.layers.Input((14, 15, 16)) y = keras.layers.GlobalMaxPooling2D()(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 14, 15, 16) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case( description=f"[keras] GlobalMaxPooling2D {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(n=5, description: str = ""): x = keras.layers.Input((3,)) y = keras.layers.RepeatVector(n=n)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 3) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case( description=f"[keras] RepeatVector {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def test(): x = keras.layers.Input((4, )) y = keras.layers.LeakyReLU()(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 4) - 0.5 vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case( description=f"[keras] LeakyReLU", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def test_itself(): x = keras.layers.Input([5, 10, 15]) y = keras.layers.Concatenate(axis=2)([x, x]) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 5, 10, 15) vy = model.predict([vx], batch_size=2) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) generate_kernel_test_case( description=f"[keras] Concat itself", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(padding=1, description: str = ""): x = keras.layers.Input((14, 15)) y = keras.layers.ZeroPadding1D(padding=padding)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 14, 15) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case( description=f"[keras] ZeroPadding1D {description}", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, EPS=1e-2)
def template(description: str = ""): x = keras.layers.Input((14, 15, 16)) y = keras.layers.GlobalAveragePooling2D()(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 14, 15, 16) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2, use_tensorflow_converter=False).convert(model) generate_kernel_test_case( description=f"[keras] GlobalAveragePooling2D {description}", graph=graph, backend=["webgpu", "webassembly", "webgl"], inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def template(input_dim=30, output_dim=10, description: str = ""): x = keras.layers.Input((14, )) y = keras.layers.Embedding(input_dim=input_dim, output_dim=output_dim)(x) model = keras.models.Model([x], [y]) vx = np.random.randint(low=0, high=input_dim, size=(2, 14)) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert(model) generate_kernel_test_case( description=f"[keras] Embedding {description}", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, )
def test(): for kwargs in [ {}, # {"data_format": "channels_first"}, # FIXME: Not Supported Yet { "data_format": "channels_last" }, { "use_bias": False }, { "activation": None }, { "padding": "valid" }, { "padding": "same" }, { "dilation_rate": (2, 2) }, ]: channels_first = ("data_format" in kwargs) and (kwargs["data_format"] == "channels_first") x = keras.layers.Input((14, 15, 16)) y = keras.layers.Conv2D(kernel_size=3, filters=8, **kwargs)(x) model = keras.models.Model([x], [y]) vx = np.random.rand(2, 14, 15, 16) vy = model.predict(vx, batch_size=2) graph = KerasConverter(batch_size=2).convert( model, input_orders=[OrderNCHW if channels_first else OrderNHWC]) generate_kernel_test_case( description="[keras] Conv2D " + (", ".join([f"{k}={v}" for k, v in kwargs.items()])), graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy}, raise_skip=False) raise SkipTest