liteRtLmGpuCachePathCheck function
The check behind liteRtLmGpuCachePathProblem. On Windows the
accelerator gets the path as UTF-8 and opens each cache through the UCRT
_open, which reads it in the ANSI code page, then CreateFileW without
the \\?\ prefix (read from the disassembly of the shipped
LiteRtWebGpuAccelerator.dll). So, unless that code page is UTF-8
(ansiCodePageIsUtf8):
- a cache directory with characters outside ASCII cannot be opened at any length. Not measured: from LiteRT-LM's source the engine create then likely fails too, on every backend, because it checks the cache directory through the ANSI code page as well;
- a model file name outside ASCII still opens, under the name its UTF-8 bytes spell in the code page, so it is no problem by itself, but its lengths are counted in those bytes. A path of 260 or more (MAX_PATH) fails on Windows, and a file name past 255 on every file system: UTF-16 code units on NTFS (UTF-8 bytes when the code page is not UTF-8, as above), UTF-8 bytes on ext4 and APFS. A failed write is said only on stderr and the create still succeeds, so without this the GPU would be cold at every start in silence.
Implementation
@visibleForTesting
LiteRtLmGpuCachePathCheck? liteRtLmGpuCachePathCheck(
String cacheDir,
String modelFileName,
int modelSize, {
required bool windows,
bool ansiCodePageIsUtf8 = false,
}) {
final names = liteRtLmGpuCacheFileNames(modelFileName, modelSize);
// What Windows counts is UTF-16 units of the path it ends up with: the
// path as it is when the code page is UTF-8, else one character per UTF-8
// byte (on a single-byte code page). Elsewhere a name's UTF-8 bytes.
int lengthOf(String s) =>
windows && ansiCodePageIsUtf8 ? s.length : utf8.encode(s).length;
final unit = windows ? 'character' : 'byte';
final dirOutsideAnsi =
windows && !ansiCodePageIsUtf8 && liteRtLmPathOutsideAscii(cacheDir);
final problems = <(Set<String>, String)>[
if (dirOutsideAnsi)
(
{'weight', 'program'},
'the cache directory has characters outside ASCII, and the '
'accelerator opens its caches through the UCRT _open, which reads '
'the UTF-8 path in the ANSI code page, so it cannot open them at '
'any length (not measured; from LiteRT-LM\'s source the engine '
'create likely fails too, on every backend)',
),
for (final (kind, name) in [
('weight', names.weight),
('program', names.program),
]) ...[
if (windows && lengthOf(cacheDir) + 1 + lengthOf(name) >= _windowsMaxPath)
(
{kind},
'its $kind cache file path would be about '
'${lengthOf(cacheDir) + 1 + lengthOf(name)} ${unit}s long, past '
"Windows' $_windowsMaxPath-character MAX_PATH",
),
if (lengthOf(name) > _maxFileNameLength)
(
{kind},
'its $kind cache file name would be about ${lengthOf(name)} '
'${unit}s long, past the $_maxFileNameLength-$unit limit on a '
'file name',
),
],
];
if (problems.isEmpty) return null;
return (
problem: problems.map((p) => p.$2).join('; '),
kinds: {for (final p in problems) ...p.$1},
cacheDirOutsideAnsi: dirOutsideAnsi,
);
}