Skip to content

Conversation

safranowith
Copy link
Contributor

Add new unary operators: FLOOR, CEIL, ROUND and TRUNC.

This PR adds support for the GGML_UNARY_OP_FLOOR, GGML_UNARY_OP_CEIL, GGML_UNARY_OP_ROUND and GGML_UNARY_OP_TRUNC operators to the GGML library.

Summary of changes:
Extended the ggml_unary_op enum and updated related arrays and assertions.
Implemented the corresponding API functions and integrated them into the codebase.
Ensured full compatibility with the existing unary operator infrastructure.

Testing:
All existing automated tests pass successfully.

Motivation:
These additions expand the mathematical capabilities of GGML, allowing users to efficiently perform operations related to decimal point manipulation—such as rounding up, down, or truncating the fractional part—on tensors.

- Added the operators to unary op enum
- Implemented API functions
- Implemented forward and unary-op logic in CPU backend
- Updated ggml_get_n_tasks
- Updated operators names array and static_assert
- Updated docs and enabled automatic tests
@github-actions github-actions bot added documentation Improvements or additions to documentation ggml changes relating to the ggml tensor library for machine learning labels Sep 18, 2025

//ggml_trunc

struct ggml_tensor * ggml_trunc(
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO the naming trunc is quite vast and can be misinterpreted as truncating only N fractional decimals. Naming like torch.frac or python's modf() makes more sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your feedback!
I have added documentation above the ggml_trunc and ggml_trunc_inplace functions in ggml.h to clarify their behavior (they truncate the fractional part towards zero, similar to std::trunc).
Please let me know if further clarification is needed.

@ggerganov
Copy link
Member

Can merge after fixing the EditorConfig CI

@safranowith safranowith requested a review from slaren as a code owner September 25, 2025 10:54
@CISC
Copy link
Collaborator

CISC commented Oct 7, 2025

This looks ready for merge after rebase, forgotten?

@CISC
Copy link
Collaborator

CISC commented Oct 9, 2025

@safranowith gentle ping

@CISC
Copy link
Collaborator

CISC commented Oct 15, 2025

@safranowith I don't think that merge went exactly as planned. :)

@safranowith
Copy link
Contributor Author

safranowith commented Oct 15, 2025 via email

@CISC
Copy link
Collaborator

CISC commented Oct 15, 2025

.All merge conflict markers have been cleaned up and the files are now updated .Let me know if the PR is now ready to be merge

A few more artifacts left to fix.

@safranowith
Copy link
Contributor Author

safranowith commented Oct 15, 2025 via email

@CISC
Copy link
Collaborator

CISC commented Oct 15, 2025

.Thanks for the feedback! I've applied the suggested changes .Please let me know if everything looks good now and if this is ready to be merged

All good now, thank you, will merge once CI is done.

GGML_UNARY_OP_FLOOR,
GGML_UNARY_OP_CEIL,
GGML_UNARY_OP_ROUND,
GGML_UNARY_OP_TRUNC,
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like a rebase artifact

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These lines are not a rebase artifact – they are newly introduced unary ops that don't exist in master.
Confirmed via git diff master -- ggml/include/ggml.h.

}
}

template<typename src_t, typename dst_t>
Copy link
Contributor

Choose a reason for hiding this comment

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

same

@safranowith
Copy link
Contributor Author

safranowith commented Oct 15, 2025 via email

@safranowith
Copy link
Contributor Author

safranowith commented Oct 15, 2025 via email

@CISC
Copy link
Collaborator

CISC commented Oct 15, 2025

Thanks again for the review and approvals! I noticed that some CI checks are failing. Just to confirm — are those failures related to my changes, or unrelated?

Unrelated.

@CISC CISC merged commit 466c191 into ggml-org:master Oct 15, 2025
68 of 71 checks passed
@safranowith
Copy link
Contributor Author

safranowith commented Oct 16, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants