Skip to content

Conversation

@taha-yassine
Copy link
Contributor

@taha-yassine taha-yassine commented Apr 29, 2025

This PR:

  • implements the missing .startswith()
  • implements step=-1 in slice
  • adds Qwen3 template

Closes #64
Addresses ggml-org/llama.cpp#13178

@taha-yassine taha-yassine changed the title Add str.startswith() Support Qwen3 (str.startswith() and [::-1]) Apr 30, 2025
@taha-yassine
Copy link
Contributor Author

I added support for step=-1 in slice since ggml-org/llama.cpp#13181 was closed.

grf53 added a commit to grf53/minja that referenced this pull request May 6, 2025
@grf53 grf53 mentioned this pull request May 8, 2025
Copy link
Contributor

@ochafik ochafik left a comment

Choose a reason for hiding this comment

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

This is awesome, thanks @taha-yassine!!!

if (auto slice = dynamic_cast<SliceExpr*>(index.get())) {
auto start = slice->start ? slice->start->evaluate(context).get<int64_t>() : 0;
auto end = slice->end ? slice->end->evaluate(context).get<int64_t>() : (int64_t) target_value.size();
bool reverse = slice->step && slice->step->evaluate(context).get<int64_t>() == -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: for readability I'd maybe first extract step (default to 1), then do the reverse + explosion if step not in (1,-1)

int64_t start = slice->start ? slice->start->evaluate(context).get<int64_t>() : (reverse ? target_value.size() - 1 : 0);
int64_t end = slice->end ? slice->end->evaluate(context).get<int64_t>() : (reverse ? -1 : target_value.size());

size_t len = target_value.size();
Copy link
Contributor

Choose a reason for hiding this comment

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

move this one up to use it instead of target_value.size() calls

@ochafik
Copy link
Contributor

ochafik commented May 15, 2025

@taha-yassine I've taken the liberty to simplify the code a bit (now supports any step != 0) + test strings, ptal (edit: ) merging now :-)

@ochafik ochafik self-requested a review May 15, 2025 10:40
@ochafik ochafik merged commit 17a767a into google:main May 15, 2025
3 of 11 checks passed
@taha-yassine
Copy link
Contributor Author

@taha-yassine I've taken the liberty to simplify the code a bit (now supports any step != 0) + test strings, ptal (edit: ) merging now :-)

Awesome thanks!

Related to the out-of-bounds issue I mentioned, it seems that your code doesn't handle that case? I wrote a little test to verify and it doesn't pass.

EXPECT_EQ(
    "[0, 1, 2, 3][0, 1, 2, 3][]",
    render("{% set x = [0, 1, 2, 3] %}{{ x[-10:] }}{{ x[:10] }}{{ x[10:20] }}", {}, {}));

Or maybe the complexity it adds isn't worth it?

ochafik added a commit to ggml-org/llama.cpp that referenced this pull request May 15, 2025
* minja: sync google/minja@f06140f

- google/minja#67 (@grf53)
- google/minja#66 (@taha-yassine)
- google/minja#63 (@grf53)
- google/minja#58

---------

Co-authored-by: ochafik <ochafik@google.com>
justinryan-0923 pushed a commit to justinryan-0923/llama.cpp that referenced this pull request May 30, 2025
* minja: sync google/minja@f06140f

- google/minja#67 (@grf53)
- google/minja#66 (@taha-yassine)
- google/minja#63 (@grf53)
- google/minja#58

---------

Co-authored-by: ochafik <ochafik@google.com>
ochafik added a commit to ochafik/minja that referenced this pull request Nov 2, 2025
* Add str.startswith()

* Add support for step=-1 in slice

* Add Qwen3 template

* Clamp out-of-bounds slice indices

* Simplify subscript logic + handle any (non-zero) step

---------

Co-authored-by: Olivier Chafik <ochafik@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Qwen3

2 participants