File size: 3,573 Bytes
5f923cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2025 The ODML Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "runtime/executor/audio_executor_settings.h"

#include <ostream>
#include <string>

#include "absl/status/status.h"  // from @com_google_absl
#include "absl/status/statusor.h"  // from @com_google_absl
#include "absl/strings/str_cat.h"  // from @com_google_absl
#include "absl/strings/string_view.h"  // from @com_google_absl
#include "runtime/executor/executor_settings_base.h"
#include "runtime/util/file_util.h"
#include "runtime/util/status_macros.h"

namespace litert::lm {

std::ostream& operator<<(std::ostream& os,
                         const AudioExecutorSettings& settings) {
  os << "AudioExecutorSettings: " << std::endl;
  os << "ModelAssets: " << settings.GetModelAssets() << std::endl;
  os << "MaxSequenceLength: " << settings.GetMaxSequenceLength() << std::endl;
  os << "Backend: " << settings.GetBackend() << std::endl;
  os << "BundledWithMainModel: " << settings.GetBundledWithMainModel()
     << std::endl;
  os << "NumThreads(CPU only): " << settings.GetNumThreads() << std::endl;
  return os;
}

absl::StatusOr<AudioExecutorSettings> AudioExecutorSettings::CreateDefault(
    const ModelAssets& model_assets, int max_sequence_length, Backend backend,
    bool bundled_with_main_model) {
  AudioExecutorSettings settings(model_assets, max_sequence_length,
                                 /*num_threads=*/4);
  RETURN_IF_ERROR(settings.SetBackend(backend));
  settings.SetBundledWithMainModel(bundled_with_main_model);
  return settings;
}

int AudioExecutorSettings::GetMaxSequenceLength() const {
  return max_sequence_length_;
}

void AudioExecutorSettings::SetMaxSequenceLength(int max_sequence_length) {
  max_sequence_length_ = max_sequence_length;
}

absl::Status AudioExecutorSettings::SetBackend(const Backend& backend) {
  if (backend != Backend::CPU && backend != Backend::GPU &&
      backend != Backend::GPU_ARTISAN) {
    return absl::InvalidArgumentError(
        "Currently AudioExecutor only supports CPU, GPU and GPU_ARTISAN.");
  }
  backend_ = backend;
  return absl::OkStatus();
}

bool AudioExecutorSettings::GetBundledWithMainModel() const {
  return bundled_with_main_model_;
}

void AudioExecutorSettings::SetBundledWithMainModel(
    bool bundled_with_main_model) {
  bundled_with_main_model_ = bundled_with_main_model;
}

absl::StatusOr<std::string> AudioExecutorSettings::GetWeightCacheFile(
    absl::string_view suffix) const {
  // Cache is explicitly disabled.
  if (GetCacheDir() == ":nocache") {
    return absl::InvalidArgumentError("Cache is explicitly disabled.");
  }
  auto model_path = GetModelAssets().GetPath().value_or("");

  // There is no model path to suffix.
  if (model_path.empty()) {
    return absl::InvalidArgumentError(
        "Cache path cannot be computed without knowing the model path.");
  }

  if (GetCacheDir().empty()) {
    return absl::StrCat(model_path, suffix);
  }

  return JoinPath(GetCacheDir(), absl::StrCat(Basename(model_path), suffix));
}

}  // namespace litert::lm