Skip to content

Commit 5d03255

Browse files
committed
test
1 parent e47591f commit 5d03255

File tree

11 files changed

+62
-116
lines changed

11 files changed

+62
-116
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/segmenter/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ zerovec = { version = "0.9.4", path = "../../utils/zerovec", features = ["yoke"]
3838
databake = { version = "0.1.3", path = "../../utils/databake", optional = true, features = ["derive"] }
3939
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"], optional = true }
4040

41-
num-traits = { version = "0.2", default-features = false, features = ["libm"], optional = true }
42-
4341
[dev-dependencies]
4442
criterion = "0.4"
4543
icu_testdata = { path = "../../provider/testdata", default-features = false, features = ["buffer", "icu_segmenter", "icu_properties"] }
@@ -56,7 +54,7 @@ default = ["auto"]
5654
std = ["icu_collections/std", "icu_locid/std", "icu_provider/std"]
5755
serde = ["dep:serde", "zerovec/serde", "icu_collections/serde", "icu_provider/serde"]
5856
datagen = ["serde", "dep:databake", "zerovec/databake", "icu_collections/databake"]
59-
lstm = ["dep:num-traits"]
57+
lstm = []
6058
auto = ["lstm"] # Enabled try_new_auto_unstable constructors
6159

6260
[lib]

components/segmenter/README.md

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/segmenter/src/complex/lstm/matrix.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,28 @@ use core::ops::Range;
88
use zerovec::ule::AsULE;
99
use zerovec::ZeroSlice;
1010

11-
// Polyfill float operations with libm in case we're no_std.
12-
#[allow(unused_imports)]
13-
use num_traits::Float;
14-
15-
/// `tanh` computes the tanh function for a scalar value.
16-
#[inline]
17-
fn tanh(x: f32) -> f32 {
18-
x.tanh()
11+
// This will be used in #[no_std] as f32::exp/f32::tanh are not in core.
12+
trait CoreFloat {
13+
fn exp(self) -> Self;
14+
fn tanh(self) -> Self;
1915
}
2016

21-
/// `sigmoid` computes the sigmoid function for a scalar value.
22-
#[inline]
23-
fn sigmoid(x: f32) -> f32 {
24-
1.0 / (1.0 + (-x).exp())
17+
impl CoreFloat for f32 {
18+
fn exp(self) -> Self {
19+
#[link(name = "m")]
20+
extern "C" {
21+
fn expf(val: f32) -> f32;
22+
}
23+
unsafe { expf(self) }
24+
}
25+
26+
fn tanh(self) -> Self {
27+
#[link(name = "m")]
28+
extern "C" {
29+
fn tanhf(val: f32) -> f32;
30+
}
31+
unsafe { tanhf(self) }
32+
}
2533
}
2634

2735
/// A `D`-dimensional, heap-allocated matrix.
@@ -217,13 +225,13 @@ impl<'a, const D: usize> MatrixBorrowedMut<'a, D> {
217225

218226
pub(super) fn sigmoid_transform(&mut self) {
219227
for x in &mut self.data.iter_mut() {
220-
*x = sigmoid(*x);
228+
*x = 1.0 / (1.0 + (-*x).exp());
221229
}
222230
}
223231

224232
pub(super) fn tanh_transform(&mut self) {
225233
for x in &mut self.data.iter_mut() {
226-
*x = tanh(*x);
234+
*x = x.tanh();
227235
}
228236
}
229237

@@ -262,7 +270,7 @@ impl<'a, const D: usize> MatrixBorrowedMut<'a, D> {
262270
// Safety: The lengths are all the same (checked above)
263271
unsafe {
264272
*self.data.get_unchecked_mut(idx) =
265-
o.get_unchecked(idx) * tanh(*c.get_unchecked(idx));
273+
o.get_unchecked(idx) * c.get_unchecked(idx).tanh();
266274
}
267275
}
268276
}

components/segmenter/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@
106106
//! ```
107107
//!
108108
//! See [`SentenceSegmenter`] for more examples.
109+
//!
110+
//! # `#![no_std]` support
111+
//!
112+
//! Like all of ICU4X, this module is `#![no_std]` by default. However, the LSTM segmenters (enabled
113+
//! with features `default`, `auto` or `lstm`) require `f32::exp` and `f32::tanh`, which are currently
114+
//! not included in `core`[^1]. Building without `std` will instead use `math.h`'s `expf` and `tanhf`
115+
//! functions, which need to be linked. There are two ways to do this:
116+
//! * If your target has a `libm` implementation, you can link with `-lm` to use native implementations.
117+
//! * If your target does not have a `libm` implementation, you can use the [`compiler_builtins`](https://github.com/rust-lang/compiler-builtins/tree/master)
118+
//! crate to provide the implementations. Keep in mind that these will usually perform worse than a
119+
//! native `libm`.
120+
//!
121+
//! [^1]: See [rust-lang/rfcs#2505](https://github.com/rust-lang/rfcs/issues/2505)
109122
110123
// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations
111124
#![cfg_attr(not(any(test, feature = "std")), no_std)]

ffi/capi_cdylib/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
//! respective library types are needed.
1313
1414
// Necessary for symbols to be linked in
15-
extern crate icu_capi;
15+
extern crate icu_capi;

ffi/gn/Cargo.lock

-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/gn/Cargo.toml

-6
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ rustflags = []
3636
[gn.package.syn."1.0.109"]
3737
rustflags = ["--cfg=syn_disable_nightly_tests"]
3838

39-
[gn.package.num-traits."0.2.15"]
40-
rustflags = ["--cfg=has_i128"]
41-
42-
[gn.package.libm."0.2.6"]
43-
rustflags = []
44-
4539
# Build cargo-gnaw with the checked-in lockfile
4640
[workspace]
4741
members = ["third_party_tools/fuchsia/tools/cargo-gnaw"]

ffi/gn/icu4x/BUILD.gn

-43
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,6 @@ rust_library("icu_segmenter-v1_2_1") {
628628
deps += [ ":icu_collections-v1_2_0" ]
629629
deps += [ ":icu_locid-v1_2_0" ]
630630
deps += [ ":icu_provider-v1_2_0" ]
631-
deps += [ ":num-traits-v0_2_15" ]
632631
deps += [ ":utf8_iter-v1_0_3" ]
633632
deps += [ ":zerovec-v0_9_4" ]
634633

@@ -738,26 +737,6 @@ rust_library("lazy_static-v1_4_0") {
738737
visibility = [ ":*" ]
739738
}
740739

741-
rust_library("libm-v0_2_6") {
742-
crate_name = "libm"
743-
crate_root = "//ffi/gn/vendor/libm/src/lib.rs"
744-
output_name = "libm-6b5214f2031c6f9e"
745-
746-
deps = []
747-
748-
rustenv = []
749-
750-
rustflags = [
751-
"--cap-lints=allow",
752-
"--edition=2018",
753-
"-Cmetadata=6b5214f2031c6f9e",
754-
"-Cextra-filename=-6b5214f2031c6f9e",
755-
"--cfg=feature=\"default\"",
756-
]
757-
758-
visibility = [ ":*" ]
759-
}
760-
761740
rust_library("litemap-v0_7_0") {
762741
crate_name = "litemap"
763742
crate_root = "//utils/litemap/src/lib.rs"
@@ -820,28 +799,6 @@ rust_library("memchr-v2_5_0") {
820799
visibility = [ ":*" ]
821800
}
822801

823-
rust_library("num-traits-v0_2_15") {
824-
crate_name = "num_traits"
825-
crate_root = "//ffi/gn/vendor/num-traits/src/lib.rs"
826-
output_name = "num_traits-64c0e09f0f13aa66"
827-
828-
deps = []
829-
deps += [ ":libm-v0_2_6" ]
830-
831-
rustenv = []
832-
833-
rustflags = [
834-
"--cap-lints=allow",
835-
"--edition=2015",
836-
"-Cmetadata=64c0e09f0f13aa66",
837-
"-Cextra-filename=-64c0e09f0f13aa66",
838-
"--cfg=feature=\"libm\"",
839-
"--cfg=has_i128",
840-
]
841-
842-
visibility = [ ":*" ]
843-
}
844-
845802
rust_library("proc-macro2-v1_0_56") {
846803
crate_name = "proc_macro2"
847804
crate_root = "//ffi/gn/vendor/proc-macro2/src/lib.rs"

tools/depcheck/src/allowlist.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ pub const EXTRA_EXPERIMENTAL_DEPS: &[&str] = &[
7171
"icu_compactdecimal",
7272
];
7373

74-
/// Dependencies allowed when opting in to LSTM segmenter
75-
pub const EXTRA_LSTM_DEPS: &[&str] = &["libm", "num-traits"];
76-
7774
/// Dependencies allowed when opting in to fixed_decimal's `ryu` backend
7875
/// This should never change
7976
pub const EXTRA_RYU_DEPS: &[&str] = &["ryu"];
@@ -122,6 +119,7 @@ pub const EXTRA_DATAGEN_DEPS: &[&str] = &[
122119
"ndarray",
123120
"num-complex",
124121
"num-integer",
122+
"num-traits",
125123
"rawpointer",
126124
"regex-syntax",
127125
"rust-format",

tools/depcheck/src/main.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ fn main() {
135135
let basic: BTreeSet<_> = basic_runtime.union(&basic_build).copied().collect();
136136
let serde: BTreeSet<_> = EXTRA_SERDE_DEPS.iter().copied().collect();
137137
let experimental: BTreeSet<_> = EXTRA_EXPERIMENTAL_DEPS.iter().copied().collect();
138-
let lstm: BTreeSet<_> = EXTRA_LSTM_DEPS.iter().copied().collect();
139138
let ryu: BTreeSet<_> = EXTRA_RYU_DEPS.iter().copied().collect();
140139
let capi_runtime: BTreeSet<_> = EXTRA_CAPI_DEPS.iter().copied().collect();
141140
let capi_build: BTreeSet<_> = EXTRA_CAPI_BUILD_DEPS.iter().copied().collect();
@@ -177,15 +176,15 @@ fn main() {
177176
"icu",
178177
"normal",
179178
"--features serde,experimental,icu_segmenter/lstm",
180-
&[&basic, &serde, &experimental, &lstm],
181-
"`EXTRA_LSTM_DEPS`",
179+
&[&basic, &serde, &experimental],
180+
"`EXTRA_EXPERIMENTAL_DEPS`",
182181
);
183182
test_dep_list(
184183
"icu_segmenter",
185184
"normal",
186185
"--features lstm",
187-
&[&basic, &lstm],
188-
"`EXTRA_LSTM_DEPS`",
186+
&[&basic],
187+
"`BASIC_RUNTIME_DEPS",
189188
);
190189
test_dep_list(
191190
"fixed_decimal",
@@ -200,53 +199,44 @@ fn main() {
200199
"normal,no-proc-macro",
201200
"",
202201
// capi should NOT pull in serde or capi-build when the proc macro is disabled
203-
&[&basic, &experimental, &lstm, &ryu, &capi_runtime],
202+
&[&basic, &experimental, &ryu, &capi_runtime],
204203
"`EXTRA_CAPI_DEPS`",
205204
);
206205

207206
test_dep_list(
208207
"icu_capi",
209208
"normal",
210209
"",
211-
&[&basic, &serde, &experimental, &lstm, &ryu, &capi],
210+
&[&basic, &serde, &experimental, &ryu, &capi],
212211
"`EXTRA_CAPI_BUILD_DEPS`",
213212
);
214213

215214
test_dep_list(
216215
"icu_capi",
217216
"normal",
218217
"--features buffer_provider",
219-
&[&basic, &serde, &experimental, &lstm, &ryu, &capi, &blob],
218+
&[&basic, &serde, &experimental, &ryu, &capi, &blob],
220219
"`EXTRA_BLOB_DEPS`",
221220
);
222221
test_dep_list(
223222
"icu_capi",
224223
"normal",
225224
"--features provider_fs",
226-
&[
227-
&basic,
228-
&serde,
229-
&experimental,
230-
&lstm,
231-
&ryu,
232-
&capi,
233-
&blob,
234-
&fs,
235-
],
225+
&[&basic, &serde, &experimental, &ryu, &capi, &blob, &fs],
236226
"`EXTRA_FS_DEPS`",
237227
);
238228
test_dep_list(
239229
"icu_capi",
240230
"normal",
241231
"--features provider_test",
242-
&[&basic, &serde, &experimental, &lstm, &ryu, &capi, &test],
232+
&[&basic, &serde, &experimental, &ryu, &capi, &test],
243233
"`EXTRA_TEST_DEPS`",
244234
);
245235
test_dep_list(
246236
"icu_capi",
247237
"normal",
248238
"--features logging",
249-
&[&basic, &serde, &experimental, &lstm, &ryu, &capi, &logging],
239+
&[&basic, &serde, &experimental, &ryu, &capi, &logging],
250240
"`EXTRA_CAPI_LOGGING_DEPS`",
251241
);
252242

@@ -258,7 +248,6 @@ fn main() {
258248
&basic,
259249
&serde,
260250
&experimental,
261-
&lstm,
262251
&blob,
263252
&fs,
264253
&zip,

0 commit comments

Comments
 (0)