File size: 2,800 Bytes
ca97aa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { DacFeatureExtractor, DacModel, DacEncoderModel, DacDecoderModel } from "../../../src/transformers.js";

import { MAX_MODEL_LOAD_TIME, MAX_TEST_EXECUTION_TIME, MAX_MODEL_DISPOSE_TIME, DEFAULT_MODEL_OPTIONS } from "../../init.js";

export default () => {
  describe("DacModel", () => {
    const model_id = "hf-internal-testing/tiny-random-DacModel";

    /** @type {DacModel} */
    let model;
    /** @type {DacFeatureExtractor} */
    let feature_extractor;
    let inputs;
    beforeAll(async () => {
      model = await DacModel.from_pretrained(model_id, DEFAULT_MODEL_OPTIONS);
      feature_extractor = await DacFeatureExtractor.from_pretrained(model_id);
      inputs = await feature_extractor(new Float32Array(12000));
    }, MAX_MODEL_LOAD_TIME);

    it(
      "forward",
      async () => {
        const { audio_values } = await model(inputs);
        expect(audio_values.dims).toEqual([1, 1, 11832]);
      },
      MAX_TEST_EXECUTION_TIME,
    );

    it(
      "encode & decode",
      async () => {
        const encoder_outputs = await model.encode(inputs);
        expect(encoder_outputs.audio_codes.dims).toEqual([1, model.config.n_codebooks, 37]);

        const { audio_values } = await model.decode(encoder_outputs);
        expect(audio_values.dims).toEqual([1, 1, 11832]);
      },
      MAX_TEST_EXECUTION_TIME,
    );

    afterAll(async () => {
      await model?.dispose();
    }, MAX_MODEL_DISPOSE_TIME);
  });

  describe("DacEncoderModel and DacDecoderModel", () => {
    const model_id = "hf-internal-testing/tiny-random-DacModel";

    /** @type {DacEncoderModel} */
    let encoder_model;
    /** @type {DacDecoderModel} */
    let decoder_model;
    /** @type {DacFeatureExtractor} */
    let feature_extractor;
    let inputs;
    let encoder_outputs;

    beforeAll(async () => {
      encoder_model = await DacEncoderModel.from_pretrained(model_id, DEFAULT_MODEL_OPTIONS);
      decoder_model = await DacDecoderModel.from_pretrained(model_id, DEFAULT_MODEL_OPTIONS);
      feature_extractor = await DacFeatureExtractor.from_pretrained(model_id);
      inputs = await feature_extractor(new Float32Array(12000));
    }, MAX_MODEL_LOAD_TIME);

    it(
      "encode",
      async () => {
        encoder_outputs = await encoder_model(inputs);
        expect(encoder_outputs.audio_codes.dims).toEqual([1, encoder_model.config.n_codebooks, 37]);
      },
      MAX_TEST_EXECUTION_TIME,
    );

    it(
      "decode",
      async () => {
        const { audio_values } = await decoder_model(encoder_outputs);
        expect(audio_values.dims).toEqual([1, 1, 11832]);
      },
      MAX_TEST_EXECUTION_TIME,
    );

    afterAll(async () => {
      await encoder_model?.dispose();
      await decoder_model?.dispose();
    }, MAX_MODEL_DISPOSE_TIME);
  });
};