Skip to content

Conversation

safranowith
Copy link

This PR introduces the TRUNC unary operator, applying element-wise truncation (truncf / sycl::trunc) on tensors.

Changes:

CPU (ggml-cpu.c)

Added forward implementation: ggml_compute_forward_trunc

Registered op in scheduler: ggml_get_n_tasks

SYCL (ggml-sycl)

Added device function: op_trunc

Added kernel: unary_op_trunc_kernel

Added dispatch wrapper: ggml_sycl_op_trunc

Exposed API: ggml_sycl_trunc

Tests

Included in backend coverage tests

Support matrix can be regenerated:

./build/bin/test-backend-ops support -o TRUNC --output csv > trunc_support.csv
./scripts/create_ops_docs.py

Produces trunc_support.csv verifying backend availability.

Notes

Implemented for F32 (and F16 where SYCL supports it)

Brings consistency with existing unary ops

@safranowith safranowith requested a review from 0cc4m as a code owner September 16, 2025 13:36
@github-actions github-actions bot added documentation Improvements or additions to documentation testing Everything test related Vulkan Issues specific to the Vulkan backend ggml changes relating to the ggml tensor library for machine learning SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language Ascend NPU issues specific to Ascend NPUs labels Sep 16, 2025
Copy link
Member

@ggerganov ggerganov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to consolidate all new unary ops PRs that are submitted recently into 2 PRs:

  • One with CPU-only implementation
  • One with SYCL implementation

The SYCL should be reviewed by @NeoZhangJianyu or someone else on the Intel team (not sure who is active these days).

Comment on lines 856 to 864

for (const std::string& btype : {"f16", "f32", "q8_1"}) {
std::vector<std::string> btypes = {"f16", "f32"};

#if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
btypes.push_back("q8_1");
#endif

for (const std::string& btype : btypes) {
for (const auto& tname : type_names) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should be in a separate PR

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my code from #15740, no idea what it is doing here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what needs fixing
This is not a change I made
I would appreciate a clearer explanation
thanks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is in 75faa5a for whatever reason, it should not be a part of that commit.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bildschirmfoto 2025-09-18 um 15 42 25

Not fully, no. You can look this up yourself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful not to add extra files that are not used in the project.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!
This has been fixed.

ggml/src/ggml.c Outdated

static const char * GGML_UNARY_OP_NAME[GGML_UNARY_OP_COUNT] = {
"ABS",
"ROUND",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"ROUND",
"TRUNC",

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error has been corrected.


enum ggml_unary_op {
GGML_UNARY_OP_ABS,
GGML_UNARY_OP_TRUNC,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new unary ops at the end of the enum.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review
This will be fixed soon

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ascend NPU issues specific to Ascend NPUs documentation Improvements or additions to documentation ggml changes relating to the ggml tensor library for machine learning SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language testing Everything test related Vulkan Issues specific to the Vulkan backend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants