Repository: xenova/transformers.js
File: tests/utils/tensor.test.js (JavaScript)

Code before:
      const t1 = new Tensor("float32", [1, 2, 3, 4, 5, 6], [3, 2]);
      const t2 = t1.slice(1);
      const target = new Tensor("float32", [3, 4], [2]);

      compare(t2, target);
    });

    it("should return a range of rows", async () => {
      const t1 = new Tensor("float32", [1, 2, 3, 4, 5, 6], [3, 2]);
      // The end index is not included.
      const t2 = t1.slice([1, 3]);
      const target = new Tensor("float32", [3, 4, 5, 6], [2, 2]);

      compare(t2, target);
    });

    it("should return a crop", async () => {
      // Create 21 nodes.
      const t1 = new Tensor("float32", Array.from({ length: 28 }, (v, i) => v = ++i), [4, 7]);
      const t2 = t1.slice([1, -1], [1, -1]);

      const target = new Tensor("float32", [9, 10, 11, 12, 13, 16, 17, 18, 19, 20], [2, 5]);

      compare(t2, target);
    });
  });

  describe("stack", () => {
    const t1 = new Tensor("float32", [0, 1, 2, 3, 4, 5], [1, 3, 2]);

    it("should stack on dim=0", async () => {
      const target1 = new Tensor("float32", [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5], [2, 1, 3, 2]);
      const target2 = new Tensor("float32", [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5], [3, 1, 3, 2]);

      // 2 tensors
      const stacked1 = stack([t1, t1], 0);
      compare(stacked1, target1, 1e-3);

      // 3 tensors
      const stacked2 = stack([t1, t1, t1], 0);
      compare(stacked2, target2, 1e-3);
    });

    it("should stack on dim=1", async () => {
      const target1 = new Tensor("float32", [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5], [1, 2, 3, 2]);
      const target2 = new Tensor("float32", [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5], [1, 3, 3, 2]);

      // 2 tensors
      const stacked1 = stack([t1, t1], 1);
      compare(stacked1, target1, 1e-3);


Review comment (suggestion):
```suggestion
  describe("slice", () => {
    it("should return a given row dim", async () => {
      const t1 = new Tensor("float32", [1, 2, 3, 4, 5, 6], [3, 2]);
      const t2 = t1.slice(1);
      const target = new Tensor("float32", [3, 4], [2]);

      compare(t2, target);
    });

    it("should return a range of rows", async () => {
      const t1 = new Tensor("float32", [1, 2, 3, 4, 5, 6], [3, 2]);
      const t2 = t1.slice([1, 3]);
      const target = new Tensor("float32", [3, 4, 5, 6], [2, 2]);

      compare(t2, target);
    });

    it("should return a crop", async () => {
      const t1 = new Tensor("float32", Array.from({ length: 28 }, (_, i) => i + 1), [4, 7]);
      const t2 = t1.slice([1, -1], [1, -1]);

      const target = new Tensor("float32", [9, 10, 11, 12, 13, 16, 17, 18, 19, 20], [2, 5]);

      compare(t2, target);
    });
  });
```

Code after:
      const t1 = new Tensor("float32", [1, 2, 3, 4, 5, 6], [3, 2]);
      const t2 = t1.slice(1);
      const target = new Tensor("float32", [3, 4], [2]);

      compare(t2, target);
    });

    it("should return a range of rows", async () => {
      const t1 = new Tensor("float32", [1, 2, 3, 4, 5, 6], [3, 2]);
      const t2 = t1.slice([1, 3]);
      const target = new Tensor("float32", [3, 4, 5, 6], [2, 2]);

      compare(t2, target);
    });

    it("should return a crop", async () => {
      const t1 = new Tensor("float32", Array.from({ length: 28 }, (_, i) => i + 1), [4, 7]);
      const t2 = t1.slice([1, -1], [1, -1]);

      const target = new Tensor("float32", [9, 10, 11, 12, 13, 16, 17, 18, 19, 20], [2, 5]);

      compare(t2, target);
    });
  });

  describe("stack", () => {
    const t1 = new Tensor("float32", [0, 1, 2, 3, 4, 5], [1, 3, 2]);

    it("should stack on dim=0", async () => {
      const target1 = new Tensor("float32", [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5], [2, 1, 3, 2]);
      const target2 = new Tensor("float32", [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5], [3, 1, 3, 2]);

      // 2 tensors
      const stacked1 = stack([t1, t1], 0);
      compare(stacked1, target1, 1e-3);

      // 3 tensors
      const stacked2 = stack([t1, t1, t1], 0);
      compare(stacked2, target2, 1e-3);
    });

    it("should stack on dim=1", async () => {
      const target1 = new Tensor("float32", [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5], [1, 2, 3, 2]);
      const target2 = new Tensor("float32", [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5], [1, 3, 3, 2]);

      // 2 tensors
      const stacked1 = stack([t1, t1], 1);
      compare(stacked1, target1, 1e-3);

      // 3 tensors
      const stacked2 = stack([t1, t1, t1], 1);