-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7628efe
commit eabd913
Showing
5 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/mediapipe-task-text/test/text_embedder_executor_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// `native-assets` tag allows test runs to opt in or out of running integration | ||
// tests via `flutter test -x native-assets` or `flutter test -t native-assets` | ||
@Tags(['native-assets']) | ||
|
||
import 'dart:io' as io; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:path/path.dart' as path; | ||
import 'package:mediapipe_core/io.dart'; | ||
import 'package:mediapipe_text/io.dart'; | ||
|
||
void main() { | ||
final pathToModel = path.joinAll([ | ||
io.Directory.current.absolute.path, | ||
'example/assets/universal_sentence_encoder.tflite', | ||
]); | ||
final modelBytes = io.File(pathToModel).readAsBytesSync(); | ||
|
||
group('TextEmbedderExecutor should', () { | ||
test('run a task', () { | ||
final executor = TextEmbedderExecutor( | ||
TextEmbedderOptions.fromAssetBuffer(modelBytes), | ||
); | ||
final TextEmbedderResult result = executor.embed('Hello, world!'); | ||
expect(result.embeddings, isNotEmpty); | ||
result.dispose(); | ||
executor.dispose(); | ||
}); | ||
|
||
test('run multiple tasks', () { | ||
final executor = TextEmbedderExecutor( | ||
TextEmbedderOptions.fromAssetBuffer(modelBytes), | ||
); | ||
final TextEmbedderResult result = executor.embed('Hello, world!'); | ||
expect(result.embeddings, isNotEmpty); | ||
final TextEmbedderResult result2 = executor.embed('Hello, world!'); | ||
expect(result2.embeddings, isNotEmpty); | ||
result.dispose(); | ||
executor.dispose(); | ||
}); | ||
|
||
test('unpack a result', () { | ||
final executor = TextEmbedderExecutor( | ||
TextEmbedderOptions.fromAssetBuffer(modelBytes), | ||
); | ||
final TextEmbedderResult result = executor.embed('Hello, world!'); | ||
final embedding = result.embeddings.first; | ||
expect(embedding.headName, 'response_encoding'); | ||
expect(embedding.quantizedEmbedding, isNull); | ||
expect(embedding.floatEmbedding, isNotNull); | ||
expect(embedding.length, 100); | ||
expect(embedding.type, equals(EmbeddingType.float)); | ||
expect(embedding.floatEmbedding![0], closeTo(1.7475, 0.0001)); | ||
result.dispose(); | ||
executor.dispose(); | ||
}); | ||
|
||
test('quantize results when requested', () { | ||
final executor = TextEmbedderExecutor( | ||
TextEmbedderOptions.fromAssetBuffer( | ||
modelBytes, | ||
embedderOptions: EmbedderOptions(quantize: true), | ||
), | ||
); | ||
final TextEmbedderResult result = executor.embed('Hello, world!'); | ||
final embedding = result.embeddings.first; | ||
expect(embedding.headName, 'response_encoding'); | ||
expect(embedding.quantizedEmbedding, isNotNull); | ||
expect(embedding.floatEmbedding, isNull); | ||
expect(embedding.quantizedEmbedding![0], 127); | ||
expect(embedding.length, 100); | ||
expect(embedding.type, equals(EmbeddingType.quantized)); | ||
result.dispose(); | ||
executor.dispose(); | ||
}); | ||
|
||
test('normalize', () { | ||
final executor = TextEmbedderExecutor( | ||
TextEmbedderOptions.fromAssetBuffer( | ||
modelBytes, | ||
embedderOptions: EmbedderOptions(l2Normalize: true), | ||
), | ||
); | ||
final TextEmbedderResult result = executor.embed('Hello, world!'); | ||
final embedding = result.embeddings.first; | ||
expect(embedding.headName, 'response_encoding'); | ||
expect(embedding.quantizedEmbedding, isNull); | ||
expect(embedding.floatEmbedding, isNotNull); | ||
expect(embedding.floatEmbedding![0], closeTo(0.1560, 0.0001)); | ||
expect(embedding.length, 100); | ||
expect(embedding.type, equals(EmbeddingType.float)); | ||
result.dispose(); | ||
executor.dispose(); | ||
}); | ||
}); | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/mediapipe-task-text/test/text_embedder_result_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:ffi'; | ||
import 'package:ffi/ffi.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mediapipe_core/src/io/test_utils.dart'; | ||
import 'package:mediapipe_text/io.dart'; | ||
import 'package:mediapipe_core/src/io/third_party/mediapipe/generated/mediapipe_common_bindings.dart' | ||
as core_bindings; | ||
import 'package:mediapipe_text/src/io/third_party/mediapipe/generated/mediapipe_text_bindings.dart' | ||
as bindings; | ||
|
||
void main() { | ||
group('TextEmbedderResult.native should', () { | ||
test('load an empty object', () { | ||
final Pointer<bindings.TextEmbedderResult> ptr = | ||
calloc<bindings.TextEmbedderResult>(); | ||
// These fields are provided by the real MediaPipe implementation, but | ||
// Dart ignores them because they are meaningless in context of text tasks | ||
ptr.ref.embeddings_count = 0; | ||
ptr.ref.has_timestamp_ms = false; | ||
|
||
final result = TextEmbedderResult.native(ptr); | ||
expect(result.embeddings, isEmpty); | ||
}); | ||
|
||
test('load a hydrated object', () { | ||
final Pointer<bindings.TextEmbedderResult> resultPtr = | ||
calloc<bindings.TextEmbedderResult>(); | ||
|
||
final embeddingsPtr = calloc<core_bindings.Embedding>(2); | ||
populateEmbedding(embeddingsPtr[0], length: 50); | ||
populateEmbedding(embeddingsPtr[1], length: 25); | ||
|
||
resultPtr.ref.embeddings_count = 2; | ||
resultPtr.ref.embeddings = embeddingsPtr; | ||
resultPtr.ref.has_timestamp_ms = false; | ||
|
||
final result = TextEmbedderResult.native(resultPtr); | ||
expect(result.embeddings, hasLength(2)); | ||
final embedding = result.embeddings.take(1).toList().first; | ||
expect(embedding.type, EmbeddingType.float); | ||
expect(embedding.length, 50); | ||
final embedding2 = result.embeddings.skip(1).take(1).toList().last; | ||
expect(embedding2.type, EmbeddingType.float); | ||
expect(embedding2.length, 25); | ||
}, timeout: const Timeout(Duration(milliseconds: 10))); | ||
}); | ||
} |