Skip to content

[ET-VK] Clean up shader library and introduce some new conventions #3024

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

Closed
wants to merge 4 commits into from

Conversation

SS-JIA
Copy link
Contributor

@SS-JIA SS-JIA commented Apr 12, 2024

Stack from ghstack (oldest at bottom):

Context

This changeset introduces some fairly mechnical improvements to the Vulkan compute graph shader library in order to introduce some new conventions.

Note that backwards compatibility with existing shader authoring methods is preserved.

Only List VALUE in the .yaml files

Previously, to generate variants for a combination of vales, the YAML file will contain

    PACKING:
      - VALUE: CHANNELS_PACKED
        SUFFIX: C_packed
      - VALUE: WIDTH_PACKED
        SUFFIX: W_packed
      - VALUE: HEIGHT_PACKED
        SUFFIX: H_packed

however, the shader code generation script will use the VALUE as the SUFFIX if no SUFFIX is provided.

Therefore, only the below is needed:

    PACKING:
      - VALUE: C_packed
      - VALUE: W_packed
      - VALUE: H_packed

Change indexing utility macros to lowercase

Indexing utility macros have been changed to lowercase, and the packing identifiers have been changed due to the change in YAML files.

The change to lowercase is to make calls to the macro read more like functions (and indeed they are typically used as functions) in order to help make the code more readable.

POS_TO_COORD_${PACKING} -> pos_to_coord_${PACKING}

Use convention of defining macros in order to reduce Python code blocks usage

Previously python code blocks were used in the GLSL code itself in order to vary the shader between different settings. However, usage of Python code blocks negatively impact code readability. Therefore, this diff seeks to introduce a convention of defining macros near the top of the shader to reduce the usage of Python code blocks, i.e.

#define pos_to_coord pos_to_coord_${PACKING}
#define get_packed_dim get_packed_dim_${PACKING}
#define get_packed_stride get_packed_stride_${PACKING}

Improve GLSL type definitions

Previously, the following Python code blocks were used to determine appropriate vectorized and scalar types:

${VEC4_T[DTYPE}} texel = ...
${T[DTYPE]} scalar = ...

This changeset replaces that with:


#define BUF_T ${buffer_scalar_type(DTYPE)}
#define VEC4_T ${texel_type(DTYPE)}
#define SCALAR_T ${texel_component_type(DTYPE)}

layout(set = 0, binding = 1) buffer  PRECISION restrict readonly Buffer {
  BUF_T data[];
}

buffer_in;
VEC4_T texel = ...
SCALAR_T scalar = ...

The main differences are as such:

  • buffer_scalar_type() produces the same result as T[DTYPE]
  • texel_type() is not determined from a mapping with DTYPE, but is determined indirectly based on the image format that is associated with the DTYPE.
  • texel_component_type() is based on the result of texel_type(DTYPE)

Essentially, the mapping is more in-line with what happens in code.

The reason for this change is to enable FP16 support and is a bit complicated. Basically, we need a way to distinguish the scalar type used for buffer storage, vs the scalar type used to store a component of a vec4 type (hence BUF_T vs SCALAR_T). The reason this is required is that to support half-precision tensors, the buffer representation will use a 16-bit float type but textures will still extract to vec4 (i.e. 4x34bit floats).

Differential Revision: D56082461

## Context

This changeset introduces some fairly mechnical improvements to the Vulkan compute graph shader library in order to introduce some new conventions.

**Note that backwards compatibility with existing shader authoring methods is preserved**.

### Only List `VALUE` in the `.yaml` files

Previously, to generate variants for a combination of vales, the YAML file will contain

```
    PACKING:
      - VALUE: CHANNELS_PACKED
        SUFFIX: C_packed
      - VALUE: WIDTH_PACKED
        SUFFIX: W_packed
      - VALUE: HEIGHT_PACKED
        SUFFIX: H_packed
```

however, the shader code generation script will use the `VALUE` as the `SUFFIX` if no `SUFFIX` is provided.

Therefore, only the below is needed:

```
    PACKING:
      - VALUE: C_packed
      - VALUE: W_packed
      - VALUE: H_packed
```

### Change indexing utility macros to lowercase

Indexing utility macros have been changed to lowercase, and the packing identifiers have been changed due to the change in YAML files.

The change to lowercase is to make calls to the macro read more like functions (and indeed they are typically used as functions) in order to help make the code more readable.

```
POS_TO_COORD_${PACKING} -> pos_to_coord_${PACKING}
```

### Use convention of defining macros in order to reduce Python code blocks usage

Previously python code blocks were used in the GLSL code itself in order to vary the shader between different settings. However, usage of Python code blocks negatively impact code readability. Therefore, this diff seeks to introduce a convention of defining macros near the top of the shader to reduce the usage of Python code blocks, i.e.

```
#define pos_to_coord pos_to_coord_${PACKING}
#define get_packed_dim get_packed_dim_${PACKING}
#define get_packed_stride get_packed_stride_${PACKING}
```

### Improve GLSL type definitions

Previously, the following Python code blocks were used to determine appropriate vectorized and scalar types:

```
${VEC4_T[DTYPE}} texel = ...
${T[DTYPE]} scalar = ...
```

This changeset replaces that with:

```

#define BUF_T ${buffer_scalar_type(DTYPE)}
#define VEC4_T ${texel_type(DTYPE)}
#define SCALAR_T ${texel_component_type(DTYPE)}

layout(set = 0, binding = 1) buffer  PRECISION restrict readonly Buffer {
  BUF_T data[];
}

buffer_in;
VEC4_T texel = ...
SCALAR_T scalar = ...
```

The main differences are as such:

* `buffer_scalar_type()` produces the same result as `T[DTYPE]`
* `texel_type()` is not determined from a mapping with `DTYPE`, but is determined indirectly based on the image format that is associated with the `DTYPE`.
* `texel_component_type()` is based on the result of `texel_type(DTYPE)`

Essentially, the mapping is more in-line with what happens in code.

The reason for this change is to enable FP16 support and is a bit complicated. Basically, we need a way to distinguish the scalar type used for buffer storage, vs the scalar type used to store a component of a vec4 type (hence `BUF_T` vs `SCALAR_T`). The reason this is required is that to support half-precision tensors, the buffer representation will use a 16-bit float type but textures will still extract to `vec4` (i.e. 4x34bit floats).

Differential Revision: [D56082461](https://our.internmc.facebook.com/intern/diff/D56082461/)

[ghstack-poisoned]
Copy link

pytorch-bot bot commented Apr 12, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/3024

Note: Links to docs will display an error until the docs builds have been completed.

❌ 4 New Failures, 1 Unrelated Failure

As of commit ec04231 with merge base c61ef44 (image):

NEW FAILURES - The following jobs have failed:

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 12, 2024
SS-JIA added a commit that referenced this pull request Apr 12, 2024
## Context

This changeset introduces some fairly mechnical improvements to the Vulkan compute graph shader library in order to introduce some new conventions.

**Note that backwards compatibility with existing shader authoring methods is preserved**.

### Only List `VALUE` in the `.yaml` files

Previously, to generate variants for a combination of vales, the YAML file will contain

```
    PACKING:
      - VALUE: CHANNELS_PACKED
        SUFFIX: C_packed
      - VALUE: WIDTH_PACKED
        SUFFIX: W_packed
      - VALUE: HEIGHT_PACKED
        SUFFIX: H_packed
```

however, the shader code generation script will use the `VALUE` as the `SUFFIX` if no `SUFFIX` is provided.

Therefore, only the below is needed:

```
    PACKING:
      - VALUE: C_packed
      - VALUE: W_packed
      - VALUE: H_packed
```

### Change indexing utility macros to lowercase

Indexing utility macros have been changed to lowercase, and the packing identifiers have been changed due to the change in YAML files.

The change to lowercase is to make calls to the macro read more like functions (and indeed they are typically used as functions) in order to help make the code more readable.

```
POS_TO_COORD_${PACKING} -> pos_to_coord_${PACKING}
```

### Use convention of defining macros in order to reduce Python code blocks usage

Previously python code blocks were used in the GLSL code itself in order to vary the shader between different settings. However, usage of Python code blocks negatively impact code readability. Therefore, this diff seeks to introduce a convention of defining macros near the top of the shader to reduce the usage of Python code blocks, i.e.

```
#define pos_to_coord pos_to_coord_${PACKING}
#define get_packed_dim get_packed_dim_${PACKING}
#define get_packed_stride get_packed_stride_${PACKING}
```

### Improve GLSL type definitions

Previously, the following Python code blocks were used to determine appropriate vectorized and scalar types:

```
${VEC4_T[DTYPE}} texel = ...
${T[DTYPE]} scalar = ...
```

This changeset replaces that with:

```

#define BUF_T ${buffer_scalar_type(DTYPE)}
#define VEC4_T ${texel_type(DTYPE)}
#define SCALAR_T ${texel_component_type(DTYPE)}

layout(set = 0, binding = 1) buffer  PRECISION restrict readonly Buffer {
  BUF_T data[];
}

buffer_in;
VEC4_T texel = ...
SCALAR_T scalar = ...
```

The main differences are as such:

* `buffer_scalar_type()` produces the same result as `T[DTYPE]`
* `texel_type()` is not determined from a mapping with `DTYPE`, but is determined indirectly based on the image format that is associated with the `DTYPE`.
* `texel_component_type()` is based on the result of `texel_type(DTYPE)`

Essentially, the mapping is more in-line with what happens in code.

The reason for this change is to enable FP16 support and is a bit complicated. Basically, we need a way to distinguish the scalar type used for buffer storage, vs the scalar type used to store a component of a vec4 type (hence `BUF_T` vs `SCALAR_T`). The reason this is required is that to support half-precision tensors, the buffer representation will use a 16-bit float type but textures will still extract to `vec4` (i.e. 4x34bit floats).

Differential Revision: [D56082461](https://our.internmc.facebook.com/intern/diff/D56082461/)

ghstack-source-id: 222379977
Pull Request resolved: #3024
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D56082461

…ventions"

## Context

This changeset introduces some fairly mechnical improvements to the Vulkan compute graph shader library in order to introduce some new conventions.

**Note that backwards compatibility with existing shader authoring methods is preserved**.

### Only List `VALUE` in the `.yaml` files

Previously, to generate variants for a combination of vales, the YAML file will contain

```
    PACKING:
      - VALUE: CHANNELS_PACKED
        SUFFIX: C_packed
      - VALUE: WIDTH_PACKED
        SUFFIX: W_packed
      - VALUE: HEIGHT_PACKED
        SUFFIX: H_packed
```

however, the shader code generation script will use the `VALUE` as the `SUFFIX` if no `SUFFIX` is provided.

Therefore, only the below is needed:

```
    PACKING:
      - VALUE: C_packed
      - VALUE: W_packed
      - VALUE: H_packed
```

### Change indexing utility macros to lowercase

Indexing utility macros have been changed to lowercase, and the packing identifiers have been changed due to the change in YAML files.

The change to lowercase is to make calls to the macro read more like functions (and indeed they are typically used as functions) in order to help make the code more readable.

```
POS_TO_COORD_${PACKING} -> pos_to_coord_${PACKING}
```

### Use convention of defining macros in order to reduce Python code blocks usage

Previously python code blocks were used in the GLSL code itself in order to vary the shader between different settings. However, usage of Python code blocks negatively impact code readability. Therefore, this diff seeks to introduce a convention of defining macros near the top of the shader to reduce the usage of Python code blocks, i.e.

```
#define pos_to_coord pos_to_coord_${PACKING}
#define get_packed_dim get_packed_dim_${PACKING}
#define get_packed_stride get_packed_stride_${PACKING}
```

### Improve GLSL type definitions

Previously, the following Python code blocks were used to determine appropriate vectorized and scalar types:

```
${VEC4_T[DTYPE}} texel = ...
${T[DTYPE]} scalar = ...
```

This changeset replaces that with:

```

#define BUF_T ${buffer_scalar_type(DTYPE)}
#define VEC4_T ${texel_type(DTYPE)}
#define SCALAR_T ${texel_component_type(DTYPE)}

layout(set = 0, binding = 1) buffer  PRECISION restrict readonly Buffer {
  BUF_T data[];
}

buffer_in;
VEC4_T texel = ...
SCALAR_T scalar = ...
```

The main differences are as such:

* `buffer_scalar_type()` produces the same result as `T[DTYPE]`
* `texel_type()` is not determined from a mapping with `DTYPE`, but is determined indirectly based on the image format that is associated with the `DTYPE`.
* `texel_component_type()` is based on the result of `texel_type(DTYPE)`

Essentially, the mapping is more in-line with what happens in code.

The reason for this change is to enable FP16 support and is a bit complicated. Basically, we need a way to distinguish the scalar type used for buffer storage, vs the scalar type used to store a component of a vec4 type (hence `BUF_T` vs `SCALAR_T`). The reason this is required is that to support half-precision tensors, the buffer representation will use a 16-bit float type but textures will still extract to `vec4` (i.e. 4x34bit floats).

Differential Revision: [D56082461](https://our.internmc.facebook.com/intern/diff/D56082461/)

[ghstack-poisoned]
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D56082461

SS-JIA added a commit that referenced this pull request Apr 13, 2024
Pull Request resolved: #3024

## Context

This changeset introduces some fairly mechnical improvements to the Vulkan compute graph shader library in order to introduce some new conventions.

**Note that backwards compatibility with existing shader authoring methods is preserved**.

### Only List `VALUE` in the `.yaml` files

Previously, to generate variants for a combination of vales, the YAML file will contain

```
    PACKING:
      - VALUE: CHANNELS_PACKED
        SUFFIX: C_packed
      - VALUE: WIDTH_PACKED
        SUFFIX: W_packed
      - VALUE: HEIGHT_PACKED
        SUFFIX: H_packed
```

however, the shader code generation script will use the `VALUE` as the `SUFFIX` if no `SUFFIX` is provided.

Therefore, only the below is needed:

```
    PACKING:
      - VALUE: C_packed
      - VALUE: W_packed
      - VALUE: H_packed
```

### Change indexing utility macros to lowercase

Indexing utility macros have been changed to lowercase, and the packing identifiers have been changed due to the change in YAML files.

The change to lowercase is to make calls to the macro read more like functions (and indeed they are typically used as functions) in order to help make the code more readable.

```
POS_TO_COORD_${PACKING} -> pos_to_coord_${PACKING}
```

### Use convention of defining macros in order to reduce Python code blocks usage

Previously python code blocks were used in the GLSL code itself in order to vary the shader between different settings. However, usage of Python code blocks negatively impact code readability. Therefore, this diff seeks to introduce a convention of defining macros near the top of the shader to reduce the usage of Python code blocks, i.e.

```
#define pos_to_coord pos_to_coord_${PACKING}
#define get_packed_dim get_packed_dim_${PACKING}
#define get_packed_stride get_packed_stride_${PACKING}
```

### Improve GLSL type definitions

Previously, the following Python code blocks were used to determine appropriate vectorized and scalar types:

```
${VEC4_T[DTYPE}} texel = ...
${T[DTYPE]} scalar = ...
```

This changeset replaces that with:

```

#define BUF_T ${buffer_scalar_type(DTYPE)}
#define VEC4_T ${texel_type(DTYPE)}
#define SCALAR_T ${texel_component_type(DTYPE)}

layout(set = 0, binding = 1) buffer  PRECISION restrict readonly Buffer {
  BUF_T data[];
}

buffer_in;
VEC4_T texel = ...
SCALAR_T scalar = ...
```

The main differences are as such:

* `buffer_scalar_type()` produces the same result as `T[DTYPE]`
* `texel_type()` is not determined from a mapping with `DTYPE`, but is determined indirectly based on the image format that is associated with the `DTYPE`.
* `texel_component_type()` is based on the result of `texel_type(DTYPE)`

Essentially, the mapping is more in-line with what happens in code.

The reason for this change is to enable FP16 support and is a bit complicated. Basically, we need a way to distinguish the scalar type used for buffer storage, vs the scalar type used to store a component of a vec4 type (hence `BUF_T` vs `SCALAR_T`). The reason this is required is that to support half-precision tensors, the buffer representation will use a 16-bit float type but textures will still extract to `vec4` (i.e. 4x34bit floats).

Differential Revision: [D56082461](https://our.internmc.facebook.com/intern/diff/D56082461/)
ghstack-source-id: 222412508
…ventions"

## Context

This changeset introduces some fairly mechnical improvements to the Vulkan compute graph shader library in order to introduce some new conventions.

**Note that backwards compatibility with existing shader authoring methods is preserved**.

### Only List `VALUE` in the `.yaml` files

Previously, to generate variants for a combination of vales, the YAML file will contain

```
    PACKING:
      - VALUE: CHANNELS_PACKED
        SUFFIX: C_packed
      - VALUE: WIDTH_PACKED
        SUFFIX: W_packed
      - VALUE: HEIGHT_PACKED
        SUFFIX: H_packed
```

however, the shader code generation script will use the `VALUE` as the `SUFFIX` if no `SUFFIX` is provided.

Therefore, only the below is needed:

```
    PACKING:
      - VALUE: C_packed
      - VALUE: W_packed
      - VALUE: H_packed
```

### Change indexing utility macros to lowercase

Indexing utility macros have been changed to lowercase, and the packing identifiers have been changed due to the change in YAML files.

The change to lowercase is to make calls to the macro read more like functions (and indeed they are typically used as functions) in order to help make the code more readable.

```
POS_TO_COORD_${PACKING} -> pos_to_coord_${PACKING}
```

### Use convention of defining macros in order to reduce Python code blocks usage

Previously python code blocks were used in the GLSL code itself in order to vary the shader between different settings. However, usage of Python code blocks negatively impact code readability. Therefore, this diff seeks to introduce a convention of defining macros near the top of the shader to reduce the usage of Python code blocks, i.e.

```
#define pos_to_coord pos_to_coord_${PACKING}
#define get_packed_dim get_packed_dim_${PACKING}
#define get_packed_stride get_packed_stride_${PACKING}
```

### Improve GLSL type definitions

Previously, the following Python code blocks were used to determine appropriate vectorized and scalar types:

```
${VEC4_T[DTYPE}} texel = ...
${T[DTYPE]} scalar = ...
```

This changeset replaces that with:

```

#define BUF_T ${buffer_scalar_type(DTYPE)}
#define VEC4_T ${texel_type(DTYPE)}
#define SCALAR_T ${texel_component_type(DTYPE)}

layout(set = 0, binding = 1) buffer  PRECISION restrict readonly Buffer {
  BUF_T data[];
}

buffer_in;
VEC4_T texel = ...
SCALAR_T scalar = ...
```

The main differences are as such:

* `buffer_scalar_type()` produces the same result as `T[DTYPE]`
* `texel_type()` is not determined from a mapping with `DTYPE`, but is determined indirectly based on the image format that is associated with the `DTYPE`.
* `texel_component_type()` is based on the result of `texel_type(DTYPE)`

Essentially, the mapping is more in-line with what happens in code.

The reason for this change is to enable FP16 support and is a bit complicated. Basically, we need a way to distinguish the scalar type used for buffer storage, vs the scalar type used to store a component of a vec4 type (hence `BUF_T` vs `SCALAR_T`). The reason this is required is that to support half-precision tensors, the buffer representation will use a 16-bit float type but textures will still extract to `vec4` (i.e. 4x34bit floats).

Differential Revision: [D56082461](https://our.internmc.facebook.com/intern/diff/D56082461/)

[ghstack-poisoned]
SS-JIA added a commit that referenced this pull request Apr 15, 2024
Pull Request resolved: #3024

## Context

This changeset introduces some fairly mechnical improvements to the Vulkan compute graph shader library in order to introduce some new conventions.

**Note that backwards compatibility with existing shader authoring methods is preserved**.

### Only List `VALUE` in the `.yaml` files

Previously, to generate variants for a combination of vales, the YAML file will contain

```
    PACKING:
      - VALUE: CHANNELS_PACKED
        SUFFIX: C_packed
      - VALUE: WIDTH_PACKED
        SUFFIX: W_packed
      - VALUE: HEIGHT_PACKED
        SUFFIX: H_packed
```

however, the shader code generation script will use the `VALUE` as the `SUFFIX` if no `SUFFIX` is provided.

Therefore, only the below is needed:

```
    PACKING:
      - VALUE: C_packed
      - VALUE: W_packed
      - VALUE: H_packed
```

### Change indexing utility macros to lowercase

Indexing utility macros have been changed to lowercase, and the packing identifiers have been changed due to the change in YAML files.

The change to lowercase is to make calls to the macro read more like functions (and indeed they are typically used as functions) in order to help make the code more readable.

```
POS_TO_COORD_${PACKING} -> pos_to_coord_${PACKING}
```

### Use convention of defining macros in order to reduce Python code blocks usage

Previously python code blocks were used in the GLSL code itself in order to vary the shader between different settings. However, usage of Python code blocks negatively impact code readability. Therefore, this diff seeks to introduce a convention of defining macros near the top of the shader to reduce the usage of Python code blocks, i.e.

```
#define pos_to_coord pos_to_coord_${PACKING}
#define get_packed_dim get_packed_dim_${PACKING}
#define get_packed_stride get_packed_stride_${PACKING}
```

### Improve GLSL type definitions

Previously, the following Python code blocks were used to determine appropriate vectorized and scalar types:

```
${VEC4_T[DTYPE}} texel = ...
${T[DTYPE]} scalar = ...
```

This changeset replaces that with:

```

#define BUF_T ${buffer_scalar_type(DTYPE)}
#define VEC4_T ${texel_type(DTYPE)}
#define SCALAR_T ${texel_component_type(DTYPE)}

layout(set = 0, binding = 1) buffer  PRECISION restrict readonly Buffer {
  BUF_T data[];
}

buffer_in;
VEC4_T texel = ...
SCALAR_T scalar = ...
```

The main differences are as such:

* `buffer_scalar_type()` produces the same result as `T[DTYPE]`
* `texel_type()` is not determined from a mapping with `DTYPE`, but is determined indirectly based on the image format that is associated with the `DTYPE`.
* `texel_component_type()` is based on the result of `texel_type(DTYPE)`

Essentially, the mapping is more in-line with what happens in code.

The reason for this change is to enable FP16 support and is a bit complicated. Basically, we need a way to distinguish the scalar type used for buffer storage, vs the scalar type used to store a component of a vec4 type (hence `BUF_T` vs `SCALAR_T`). The reason this is required is that to support half-precision tensors, the buffer representation will use a 16-bit float type but textures will still extract to `vec4` (i.e. 4x34bit floats).
ghstack-source-id: 222535266

Differential Revision: [D56082461](https://our.internmc.facebook.com/intern/diff/D56082461/)
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D56082461

…ventions"

## Context

This changeset introduces some fairly mechnical improvements to the Vulkan compute graph shader library in order to introduce some new conventions.

**Note that backwards compatibility with existing shader authoring methods is preserved**.

### Only List `VALUE` in the `.yaml` files

Previously, to generate variants for a combination of vales, the YAML file will contain

```
    PACKING:
      - VALUE: CHANNELS_PACKED
        SUFFIX: C_packed
      - VALUE: WIDTH_PACKED
        SUFFIX: W_packed
      - VALUE: HEIGHT_PACKED
        SUFFIX: H_packed
```

however, the shader code generation script will use the `VALUE` as the `SUFFIX` if no `SUFFIX` is provided.

Therefore, only the below is needed:

```
    PACKING:
      - VALUE: C_packed
      - VALUE: W_packed
      - VALUE: H_packed
```

### Change indexing utility macros to lowercase

Indexing utility macros have been changed to lowercase, and the packing identifiers have been changed due to the change in YAML files.

The change to lowercase is to make calls to the macro read more like functions (and indeed they are typically used as functions) in order to help make the code more readable.

```
POS_TO_COORD_${PACKING} -> pos_to_coord_${PACKING}
```

### Use convention of defining macros in order to reduce Python code blocks usage

Previously python code blocks were used in the GLSL code itself in order to vary the shader between different settings. However, usage of Python code blocks negatively impact code readability. Therefore, this diff seeks to introduce a convention of defining macros near the top of the shader to reduce the usage of Python code blocks, i.e.

```
#define pos_to_coord pos_to_coord_${PACKING}
#define get_packed_dim get_packed_dim_${PACKING}
#define get_packed_stride get_packed_stride_${PACKING}
```

### Improve GLSL type definitions

Previously, the following Python code blocks were used to determine appropriate vectorized and scalar types:

```
${VEC4_T[DTYPE}} texel = ...
${T[DTYPE]} scalar = ...
```

This changeset replaces that with:

```

#define BUF_T ${buffer_scalar_type(DTYPE)}
#define VEC4_T ${texel_type(DTYPE)}
#define SCALAR_T ${texel_component_type(DTYPE)}

layout(set = 0, binding = 1) buffer  PRECISION restrict readonly Buffer {
  BUF_T data[];
}

buffer_in;
VEC4_T texel = ...
SCALAR_T scalar = ...
```

The main differences are as such:

* `buffer_scalar_type()` produces the same result as `T[DTYPE]`
* `texel_type()` is not determined from a mapping with `DTYPE`, but is determined indirectly based on the image format that is associated with the `DTYPE`.
* `texel_component_type()` is based on the result of `texel_type(DTYPE)`

Essentially, the mapping is more in-line with what happens in code.

The reason for this change is to enable FP16 support and is a bit complicated. Basically, we need a way to distinguish the scalar type used for buffer storage, vs the scalar type used to store a component of a vec4 type (hence `BUF_T` vs `SCALAR_T`). The reason this is required is that to support half-precision tensors, the buffer representation will use a 16-bit float type but textures will still extract to `vec4` (i.e. 4x34bit floats).

Differential Revision: [D56082461](https://our.internmc.facebook.com/intern/diff/D56082461/)

[ghstack-poisoned]
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D56082461

SS-JIA added a commit that referenced this pull request Apr 15, 2024
Pull Request resolved: #3024

## Context

This changeset introduces some fairly mechnical improvements to the Vulkan compute graph shader library in order to introduce some new conventions.

**Note that backwards compatibility with existing shader authoring methods is preserved**.

### Only List `VALUE` in the `.yaml` files

Previously, to generate variants for a combination of vales, the YAML file will contain

```
    PACKING:
      - VALUE: CHANNELS_PACKED
        SUFFIX: C_packed
      - VALUE: WIDTH_PACKED
        SUFFIX: W_packed
      - VALUE: HEIGHT_PACKED
        SUFFIX: H_packed
```

however, the shader code generation script will use the `VALUE` as the `SUFFIX` if no `SUFFIX` is provided.

Therefore, only the below is needed:

```
    PACKING:
      - VALUE: C_packed
      - VALUE: W_packed
      - VALUE: H_packed
```

### Change indexing utility macros to lowercase

Indexing utility macros have been changed to lowercase, and the packing identifiers have been changed due to the change in YAML files.

The change to lowercase is to make calls to the macro read more like functions (and indeed they are typically used as functions) in order to help make the code more readable.

```
POS_TO_COORD_${PACKING} -> pos_to_coord_${PACKING}
```

### Use convention of defining macros in order to reduce Python code blocks usage

Previously python code blocks were used in the GLSL code itself in order to vary the shader between different settings. However, usage of Python code blocks negatively impact code readability. Therefore, this diff seeks to introduce a convention of defining macros near the top of the shader to reduce the usage of Python code blocks, i.e.

```
#define pos_to_coord pos_to_coord_${PACKING}
#define get_packed_dim get_packed_dim_${PACKING}
#define get_packed_stride get_packed_stride_${PACKING}
```

### Improve GLSL type definitions

Previously, the following Python code blocks were used to determine appropriate vectorized and scalar types:

```
${VEC4_T[DTYPE}} texel = ...
${T[DTYPE]} scalar = ...
```

This changeset replaces that with:

```

#define BUF_T ${buffer_scalar_type(DTYPE)}
#define VEC4_T ${texel_type(DTYPE)}
#define SCALAR_T ${texel_component_type(DTYPE)}

layout(set = 0, binding = 1) buffer  PRECISION restrict readonly Buffer {
  BUF_T data[];
}

buffer_in;
VEC4_T texel = ...
SCALAR_T scalar = ...
```

The main differences are as such:

* `buffer_scalar_type()` produces the same result as `T[DTYPE]`
* `texel_type()` is not determined from a mapping with `DTYPE`, but is determined indirectly based on the image format that is associated with the `DTYPE`.
* `texel_component_type()` is based on the result of `texel_type(DTYPE)`

Essentially, the mapping is more in-line with what happens in code.

The reason for this change is to enable FP16 support and is a bit complicated. Basically, we need a way to distinguish the scalar type used for buffer storage, vs the scalar type used to store a component of a vec4 type (hence `BUF_T` vs `SCALAR_T`). The reason this is required is that to support half-precision tensors, the buffer representation will use a 16-bit float type but textures will still extract to `vec4` (i.e. 4x34bit floats).
ghstack-source-id: 222551445

Differential Revision: [D56082461](https://our.internmc.facebook.com/intern/diff/D56082461/)
@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 59023ed.

@mergennachin mergennachin mentioned this pull request Apr 26, 2024
@SS-JIA SS-JIA deleted the gh/SS-JIA/33/head branch January 24, 2025 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants