Skip to content

[ET-VK] double, short, and uint16 dtype runtime support #11670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: gh/ahmtox/17/orig
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 103 additions & 33 deletions backends/vulkan/runtime/gen_vulkan_spv.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,52 +56,97 @@
TYPE_MAPPINGS: Dict[str, Any] = {
"IMAGE_T": {
3: {
"double": "image3D",
"float": "image3D",
"half": "image3D",
"int": "iimage3D",
"uint": "uimage3D",
# integer dtypes
"int8": "iimage3D",
"uint8": "uimage3D",
"int16": "iimage3D",
"uint16": "uimage3D",
"int32": "iimage3D",
"uint32": "uimage3D",
"int64": "iimage3D",
"uint64": "uimage3D",
# common dtype aliases
"bool": "uimage3D",
"int": "iimage3D",
"uint": "uimage3D",
},
2: {
"double": "image2D",
"float": "image2D",
"half": "image2D",
"int": "iimage2D",
"uint": "uimage2D",
# integer dtypes
"int8": "iimage2D",
"uint8": "uimage2D",
"int16": "iimage2D",
"uint16": "uimage2D",
"int32": "iimage2D",
"uint32": "uimage2D",
"int64": "iimage2D",
"uint64": "uimage2D",
# common dtype aliases
"bool": "uimage2D",
"int": "iimage2D",
"uint": "uimage2D",
},
},
"SAMPLER_T": {
3: {
"double": "sampler3D",
"float": "sampler3D",
"half": "sampler3D",
"int": "isampler3D",
"uint": "usampler3D",
# integer dtypes
"int8": "isampler3D",
"uint8": "usampler3D",
"int16": "isampler3D",
"uint16": "usampler3D",
"int32": "isampler3D",
"uint32": "usampler3D",
"int64": "isampler3D",
"uint64": "usampler3D",
# common dtype aliases
"bool": "usampler3D",
"int": "isampler3D",
"uint": "usampler3D",
},
2: {
"double": "sampler2D",
"float": "sampler2D",
"half": "sampler2D",
"int": "isampler2D",
"uint": "usampler2D",
# integer dtypes
"int8": "isampler2D",
"uint8": "usampler2D",
"int16": "isampler2D",
"uint16": "usampler2D",
"int32": "isampler2D",
"uint32": "usampler2D",
"int64": "isampler2D",
"uint64": "usampler2D",
# common dtype aliases
"bool": "usampler2D",
"int": "isampler2D",
"uint": "usampler2D",
},
},
"IMAGE_FORMAT": {
"double": "rgba32f",
"float": "rgba32f",
"half": "rgba16f",
"int": "rgba32i",
"uint": "rgba32ui",
# integer dtypes
"int8": "rgba8i",
"uint8": "rgba8ui",
"int16": "rgba16i",
"uint16": "rgba16ui",
"int32": "rgba32i",
"uint32": "rgba32ui",
"int64": "rgba32i",
"uint64": "rgba32ui",
# common dtype aliases
"bool": "rgba8ui",
"int": "rgba32i",
"uint": "rgba32ui",
},
}

Expand All @@ -118,33 +163,47 @@ def define_variable(name: str) -> str:
def buffer_scalar_type(dtype: str) -> str:
if dtype == "half":
return "float16_t"
elif dtype[-1] == "8":
return dtype + "_t"
elif dtype == "float":
return "float"
elif dtype == "double":
return "float64_t"
# integer dtype alias conversion
elif dtype == "bool":
return "uint8_t"
# we don't want to append _t for int32 or uint32 as int is already 32bit
elif dtype == "int32" or dtype == "uint32":
return "int" if dtype == "int32" else "uint"
elif dtype[-1].isdigit():
return dtype + "_t"
return dtype


def buffer_gvec_type(dtype: str, n: int) -> str:
if n == 1:
return buffer_scalar_type(dtype)

if dtype == "float":
return f"vec{n}"
if dtype == "uint":
return f"uvec{n}"
elif dtype == "half":
return f"f16vec{n}"
elif dtype == "int":
return f"ivec{n}"
elif dtype == "int8":
return f"i8vec{n}"
elif dtype == "uint8":
return f"u8vec{n}"
elif dtype == "bool":
return f"u8vec{n}"

raise AssertionError(f"Invalid dtype: {dtype}")
dtype_map = {
"half": f"f16vec{n}",
"float": f"vec{n}",
"double": f"vec{n}", # No 64bit image format support in GLSL
"int8": f"i8vec{n}",
"uint8": f"u8vec{n}",
"int16": f"i16vec{n}",
"uint16": f"u16vec{n}",
"int32": f"ivec{n}",
"int": f"ivec{n}",
"uint32": f"uvec{n}",
"uint": f"uvec{n}",
"int64": f"ivec{n}", # No 64bit image format support in GLSL
"uint64": f"uvec{n}", # No 64bit image format support in GLSL
"bool": f"u8vec{n}",
}

vector_type = dtype_map.get(dtype)
if vector_type is None:
raise AssertionError(f"Invalid dtype: {dtype}")

return vector_type


def texel_type(dtype: str) -> str:
Expand Down Expand Up @@ -365,15 +424,22 @@ def define_required_extensions(dtypes: Union[str, List[str]]):
if dtype == "half":
nbit = "16bit"
glsl_type = "float16"
elif dtype == "int16" or dtype == "uint16":
nbit = "16bit"
glsl_type = "int16"
elif dtype == "int8" or dtype == "uint8" or dtype == "bool":
elif dtype == "double":
# We only need to allow float64_t type usage
glsl_type = "float64"
elif dtype in ["int8", "uint8", "bool"]:
nbit = "8bit"
glsl_type = "int8"
elif dtype in ["int16", "uint16"]:
nbit = "16bit"
glsl_type = "int16"
elif dtype in ["int64", "uint64"]:
# We only need to allow int64_t and uint64_t type usage
glsl_type = "int64"

if nbit is not None and glsl_type is not None:
if nbit is not None:
out_str += f"#extension GL_EXT_shader_{nbit}_storage : require\n"
if glsl_type is not None:
out_str += f"#extension GL_EXT_shader_explicit_arithmetic_types_{glsl_type} : require\n"

return out_str
Expand Down Expand Up @@ -629,6 +695,10 @@ def generateVariantCombinations(

elif "VALUE" in value:
suffix = value.get("SUFFIX", value["VALUE"])
if value["VALUE"] in ["int", "uint"]:
raise ValueError(
f"Use int32 or uint32 instead of {value['VALUE']}"
)
param_values.append((param_name, suffix, value["VALUE"]))

else:
Expand Down
4 changes: 2 additions & 2 deletions backends/vulkan/runtime/graph/ops/glsl/arange.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
arange:
parameter_names_with_default_values:
NDIM: 3
DTYPE: int
DTYPE: int32
STORAGE: texture3d
PACKING: C_packed
generate_variant_forall:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: int32
shader_variants:
- NAME: arange
2 changes: 1 addition & 1 deletion backends/vulkan/runtime/graph/ops/glsl/avg_pool2d.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ avg_pool2d:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: int32
shader_variants:
- NAME: avg_pool2d
2 changes: 1 addition & 1 deletion backends/vulkan/runtime/graph/ops/glsl/binary_op.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ binary_op:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: int32
shader_variants:
- NAME: binary_add
- NAME: binary_sub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ buffer_to_buffer:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: double
- VALUE: int8
- VALUE: uint8
- VALUE: int32
shader_variants:
- NAME: buffer_to_buffer
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ buffer_to_nchw:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: double
- VALUE: int8
- VALUE: uint8
- VALUE: int32
shader_variants:
- NAME: buffer_to_nchw
- NAME: buffer_to_nchw_no_pc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ copy_channel_offset:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: int32
shader_variants:
- NAME: copy_channel_offset
2 changes: 1 addition & 1 deletion backends/vulkan/runtime/graph/ops/glsl/copy_offset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ copy_offset:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: int32
- VALUE: int8
- VALUE: uint8
STORAGE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ copy_packed_dim_offset:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: int32
shader_variants:
- NAME: copy_packed_dim_offset
2 changes: 1 addition & 1 deletion backends/vulkan/runtime/graph/ops/glsl/embedding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ embedding:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: int32
shader_variants:
- NAME: embedding
3 changes: 2 additions & 1 deletion backends/vulkan/runtime/graph/ops/glsl/flip.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ flip:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: double
- VALUE: int8
- VALUE: uint8
- VALUE: int32
shader_variants:
- NAME: flip
3 changes: 2 additions & 1 deletion backends/vulkan/runtime/graph/ops/glsl/image_to_nchw.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ image_to_nchw:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: double
- VALUE: int8
- VALUE: uint8
- VALUE: int32
shader_variants:
- NAME: image_to_nchw_texture3d
- NAME: image_to_nchw_texture2d
Expand Down
2 changes: 1 addition & 1 deletion backends/vulkan/runtime/graph/ops/glsl/index_select.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ index_select:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: int32
shader_variants:
- NAME: index_select
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ index_select_channel:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: int32
shader_variants:
- NAME: index_select_channel
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ nchw_to_buffer:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: double
- VALUE: int8
- VALUE: uint8
- VALUE: int32
shader_variants:
- NAME: nchw_to_buffer
- NAME: nchw_to_buffer_no_pc
Expand Down
6 changes: 5 additions & 1 deletion backends/vulkan/runtime/graph/ops/glsl/nchw_to_image.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,9 @@ void main() {
return;
}

write_texel(t_out, lpos_to_pos(lpos, axis_map), read_texel(tidx));
$if DTYPE == "double" and DTYPE == "int64":
VEC4_T texel = read_texel(tidx);
write_texel(t_out, lpos_to_pos(lpos, axis_map), texel);
$else:
write_texel(t_out, lpos_to_pos(lpos, axis_map), read_texel(tidx));
}
3 changes: 2 additions & 1 deletion backends/vulkan/runtime/graph/ops/glsl/nchw_to_image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ nchw_to_image:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: double
- VALUE: int8
- VALUE: uint8
- VALUE: int32
shader_variants:
- NAME: nchw_to_image_texture3d
- NAME: nchw_to_image_texture2d
Expand Down
2 changes: 1 addition & 1 deletion backends/vulkan/runtime/graph/ops/glsl/no_op.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ no_op:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: int32
- VALUE: int8
- VALUE: uint8
STORAGE:
Expand Down
2 changes: 1 addition & 1 deletion backends/vulkan/runtime/graph/ops/glsl/permute.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ permute:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int
- VALUE: int32
shader_variants:
- NAME: permute
Loading
Loading